A. The Monster

Codeforces
IDCF787A
Time1000ms
Memory256MB
Difficulty
brute forcemathnumber theory
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_, .... <center>![image](https://espresso.codeforces.com/fa87bf740de4111cfce3f7bda62866ec8e2470d0.png)</center>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 The first line of input contains two integers _a_ and _b_ (1 ≤ _a_, _b_ ≤ 100). The second line contains two integers _c_ and _d_ (1 ≤ _c_, _d_ ≤ 100). ## Output Print the first time Rick and Morty will scream at the same time, or  - 1 if they will never scream at the same time. [samples] ## Note 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.
Samples
Input #1
20 2
9 19
Output #1
82
Input #2
2 1
16 12
Output #2
\-1
API Response (JSON)
{
  "problem": {
    "name": "A. The Monster",
    "description": {
      "content": "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 Mo",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF787A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 Mo...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments