787A.md (1464B)
1 Source: https://codeforces.com/problemset/problem/787/A 2 3 A. The Monster 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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* + 2*a*, *b* + 3*a*, ... and Morty screams at times *d*, *d* + *c*, *d* + 2*c*, *d* + 3*c*, .... 22 23  24 25 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. 26 27 Input 28 29 The first line of input contains two integers *a* and *b* (1 ≤ *a*, *b* ≤ 100). 30 31 The second line contains two integers *c* and *d* (1 ≤ *c*, *d* ≤ 100). 32 33 Output 34 35 Print the first time Rick and Morty will scream at the same time, or - 1 if they will never scream at the same time. 36 37 Examples 38 39 Input 40 41 ``` 42 20 2 43 9 19 44 ``` 45 46 Output 47 48 ``` 49 82 50 ``` 51 52 Input 53 54 ``` 55 2 1 56 16 12 57 ``` 58 59 Output 60 61 ``` 62 -1 63 ``` 64 65 Note 66 67 In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82. 68 69 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.