Jump Jump

Time limit1sMemory limit256 MB

Summary
Given jump lengths X and Y plus start positions P1 and P2, find the smallest common landing point both jumpers reach, or -1 if none exists.
Level

Medium4 of 10

Topics
Math, Number theory, Brute force, Implementation
Solved
No attempts yet

Problem

Two students A and B are long jumping in the same direction along a straight track. A jumps X meters at a time, and B jumps Y meters at a time. Given the starting points of the two students and the values X and Y, write a program to find the point that both students pass through and that is closest to the starting points.

For example, suppose A, who jumps 10 meters at a time, starts long jumping at point 30, and B, who jumps 12 meters at a time, starts at point 8. If A makes 5 long jumps and B makes 6 long jumps, the two pass through point 80 in common, and this is the common point closest to the starting points.

Input

The first line gives the distances X and Y that the two people jump at a time, and the starting positions P1 and P2, separated by spaces as natural numbers. (1 ≤ X, Y, P1, P2 ≤ 100)

Output

On the first line, print the point closest to the starting points among the points that both students pass through in common.

If there is no point that both students pass through in common, print -1.

Examples3

  1. Example 1

    Input
    10 12 30 8
    
    Expected output
    80
    
  2. Example 2

    Input
    1 1 7 12
    
    Expected output
    12
    
  3. Example 3

    Input
    7 7 2 1
    
    Expected output
    -1