A, B = gets.split.map(&:to_i) T = 60 * 60 * 24 def f(t) t * Rational(360, 60 * 60) end def g(t) t * Rational(360, 60 * 60 * 12) end def t1(m, s) (m * 60 + s) % 3600 end def t2(h, m, s) ((h * 60 * 60) + m * 60 + s) % (60 * 60 * 12) end 0.upto(7200) do |i| a = f(t1(B, i)) b = g(t2(A, B, i)) c = f(t1(B, i + 1)) d = g(t2(A, B, i)) if a <= b && c >= d puts i exit end end