1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
x,d,h,m = (int(x) for x in input().split())

round_dhm = [[24, 24, 0],
             [25, 24, 0],
             [26, 24, 0],
             [27, 24, 0],
             [29, 24, 0]]
res = (round_dhm[x-1][0] - d) * 60 * 24 + (round_dhm[x-1][1] - h) * 60 + round_dhm[x-1][2] - m
if x == 5 and d < 29 or d == 29 and h < 2:
    res -= 60

print(res)