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


start_timestamps = [0, 1440, 2880, 4320, 5760]

end_timestamps = [2160, 3600, 5040, 6480, 9360]

custom_start_timestamp = ((d - 23) * 1440) + (h * 60) + m - 720
# print(custom_start_timestamp)

if x == 5 and custom_start_timestamp <= 8040:
    custom_start_timestamp += 60

print(end_timestamps[x - 1] - custom_start_timestamp)