s = gets.to_s.split.map(&:to_i) time = (s[1] * 60 + s[2] + s[3] * (s[0] - 1)) % 1440 puts time / 60 puts time % 60