Олимпиадный тренинг

Задача 34774. Troika


Задача

Темы:
Oleg has a Troika card, which has one trip left on land transport. You can get from Oleg's house to the school by tram, trolleybus or bus. The tram runs every 15 minutes, the trolleybus — every 10 minutes, bus — every 5 minutes, while at 8:00 a tram, a trolleybus, and a bus depart from the stop at the same time (that is, the tram leaves at 8:00, 8:15, 8:30, 8:45, 9:00; trolleybus — at 8:00, 8:10, 8:20, 8:30, 8:40, 8:50, 9:00; bus — at 8:00, 8:05, 8:10, 8:15 etc.). The tram travels to the desired stop X minutes, the trolleybus — Y minutes, bus — Z minutes.

When Oleg came to the stop, it was 8 hours M minutes on the clock. Determine the minimum time after which Oleg will be at the stop he needs (counting the waiting time for the transport and the time of the trip by transport). If some transport leaves at the same moment when Oleg came to the stop, then Oleg manages to leave on it.
The program first receives as input three positive integers X, Y, Z, not exceeding 100, written in separate lines, — travel time by tram, trolleybus, bus, respectively. The fourth line of the input contains an integer M (0 ≤ M ≤ 59) — time (in minutes) when Oleg came to the bus stop.
The program should output one natural number — the minimum possible total waiting time for transport and trip.
 
Input Output Note
25
10
20
12
18 Oleg came to the bus stop at 8:12. He needs to wait 8 minutes and take a trolleybus, which will take him in 10 minutes.