1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Author: Bartek Knapik

last_day = [0, 24, 25, 26, 27, 29]

def get_ans(round, dd, hh, mm):
    res = 0
    if round == 5:
        if dd != last_day[round] or (dd == last_day[round] and hh < 2):
            hh += 1
    res += (last_day[round] - dd) * 24 * 60
    res += (23 - hh) * 60
    res += (60 - mm)
              
    return res

f_in = [int(el) for el in input().split()]

print(get_ans(*f_in))