1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def get_time(day, hour, minute):
    return (day * 24 + hour) * 60 + minute


def get_round_end(round):
    day = round + 23
    if round == 5:
        day += 1
    hour = 24
    minute = 0
    return get_time(day, hour, minute)


date = input()
x, d, h, m = [int(x) for x in date.split()]
start_time = get_time(d, h, m)
end_time = get_round_end(x)
result = end_time - start_time
time_skip = get_time(29, 2, 0)

if start_time < time_skip and time_skip < end_time:
    result -= 60

print(result)