1883.The Monster

Time Limit: 1s Memory Limit: 256MB

A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b,b+a,b+2a,b+3a,... and Morty screams at times d,d+c,d+2c,d+3c,....

1883_1.png

The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time.

Input Format(From the terminal/stdin)

The first line of input contains two integers a and b (1 \le a,b \le 100).

The second line contains two integers c and d (1 \le c,d \le 100).

Output Format(To the terminal/stdout)

Print the first time Rick and Morty will scream at the same time, or -1 if they will never scream at the same time.

Sample Input 1

Copy
20 2
9 19
  · \n
 ·  \n

Sample Output 1

Copy
82
  \n

Sample Input 2

Copy
2 1
16 12
 · \n
  ·  \n

Sample Output 2

Copy
-1
  \n

Hints

In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82.

In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time.

Submit

请先 登录

© 2025 FAQs