n = gets.to_i total = 0 ans = 0 n.times do h1, m1, h2, m2 = gets.split(/:|\s/).map(&:to_i) total = (h2 - h1) * 60 + m2 - m1 total += 1440 if total < 0 ans += total end puts ans