q=gets.to_i n,h,m,t="1000000 23 59 1440".split.map(&:to_i) o=Time::local(1,1,1,h,m)+(n-1)*t*60+(q-2).to_i abort unless [o.hour,o.min] == [23,59] p 0