1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
x, d, h, m = map(int, input().split())


def mins_till_midnight(h, m):
    return (24 - h) * 60 - m


result = mins_till_midnight(h, m)

if x == 5:
    if d - x == 22:
        result += 2 * 24 * 60
    if d - x == 23:
        result += 24 * 60
    if d < 29 or h <= 2:
        result -= 60
else:
    if d - x == 22:
        result += 24 * 60


print(result)