1
2
3
4
5
6
7
8
x, d, h, m = map(int, input().split())

def to_mins(d, h, m):
    offset = -60 if d == 29 and h >= 3 else 0
    return (d * 24 + h) * 60 + m + offset


print(-to_mins(d, h, m) + to_mins([-1, 24, 25, 26, 27, 29][x], 23, 59) + 1)