n = gets.chomp.to_i sum = 0 n.times { line = gets.chomp.split begin_h, begin_m = line[0].split(':').map &:to_i end_h, end_m = line[1].split(':').map &:to_i if begin_h > end_h begin_h -= 24 elsif begin_h == end_h if begin_m > end_m begin_m -= 24 * 60 end end minute_begin = begin_h * 60 + begin_m minute_end = end_h * 60 + end_m time = minute_end - minute_begin sum += time } p sum