LearnPy Hub
← Arena
AdvancedRating 1800

Two Remainders at Once

Find the smallest non-negative x with x ≡ a (mod m) and x ≡ b (mod n). Print -1 if no such x exists.

Input

Four integers a m b n (1 ≤ m, n ≤ 10⁹, 0 ≤ a < m, 0 ≤ b < n).

Output

One integer.

Sample 1

Input
2 3 3 5
Output
8

Sample 2

Input
1 4 2 6
Output
-1
solution.py
Output appears here
Two Remainders at Once · Python Arena problem · LearnPy Hub