n, h, m, t = gets.split.map(&:to_i) min = (h * 60 + m + t * (n - 1)) % 1440 puts min.divmod(60)