#P3409. Duty Rotation Convergence
Duty Rotation Convergence
Duty Rotation Convergence
In a semester there are daily duty monitors ((\textit{(D)})) which rotate cyclically, and among them student A holds the (m)th position. In addition, there are weekly duty monitors ((\textit{(W)})) rotating week by week, with student B being the (q)th in order. The semester is organized in weeks with 5 working days and 2 rest days (holidays are ignored), and the order of rotation is never changed. The daily duty monitor is chosen on each working day in a cyclic order (i.e. the schedule repeats every duty assignments) and the weekly duty monitor is assigned once per week (the schedule repeats every weeks).
On the (i)th working day of the semester (with (i \ge 1)), the duty monitor is given by
[
D(i)=((i-1)\bmod n)+1,
]
and for the week number (w=\lfloor(i-1)/5\rfloor+1) the weekly duty monitor is
[
W(w)=((w-1)\bmod p)+1.
]
Your task is to determine the first working day (i.e. the smallest positive integer (i)) on which student A is the daily duty monitor and simultaneously student B is the weekly duty monitor. If no such day exists, output exactly Orz mgh!!!
.
inputFormat
The input consists of four space-separated integers: (n), (m), (p), and (q), representing the total number of daily duty monitors, the position of A among them, the total number of weekly duty monitors, and the position of B among them respectively.
outputFormat
Output a single line containing the first working day number (i.e. (i)) on which student A and student B are on duty simultaneously. If no such day exists, output Orz mgh!!!
.
sample
6 3 4 2
9