Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/integer-keyingi-avtobus

Next bus

Medium 1000 ms 64 MB 39%
Share

The first bus leaves the stop at $h_{1}$ hours $m_{1}$ minutes, and then a bus leaves every $d$ minutes. A passenger came to the stop on the same day at $h_{2}$ hours $m_{2}$ minutes. Find how many minutes they will wait for the next bus. If a bus leaves exactly at the minute the passenger arrives, the wait is $0$ (the passenger makes it). For example, if the first bus leaves at $6{:}00$ and $d = 15$, buses leave at $6{:}00$, $6{:}15$, $6{:}30$, …; a passenger who arrives at $6{:}20$ waits $10$ minutes.

Bus intervals and the passenger's waiting time

Input

The first line contains three integers: $h_{1}$, $m_{1}$ and $d$.
The second line contains two integers: $h_{2}$ and $m_{2}$.

Output

One integer — the waiting time in minutes.

Note

$0 \le h_{1}, h_{2} \le 23$
$0 \le m_{1}, m_{2} \le 59$
$1 \le d \le 1440$
$h_{2}$ hours $m_{2}$ minutes is not earlier than $h_{1}$ hours $m_{1}$ minutes.

Examples

Input #1
6 0 15
6 20
Output #1
10
Input #2
7 30 25
8 0
Output #2
20

Sign in to submit a solution

Signing up is free and takes a minute.