PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000309. Ngủ quên (Oversleeping)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ngủ quên (Oversleeping)

Oversleeping

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A train repeatedly travels between towns AA and BB. At time 00 it leaves AA, then travels to BB for XX seconds, stops at BB for YY seconds, travels back to AA for XX seconds, and stops at AA for YY seconds.

A passenger boards the train at time 00. The passenger repeatedly sleeps for PP seconds and stays awake for QQ seconds, starting asleep. The passenger may get off only when the train is stopped at BB and the passenger is awake.

All time intervals are half-open: the left endpoint is included and the right endpoint is excluded. For integer u≥0u\ge0, the train stops at BB during [(2X+2Y)u+X, (2X+2Y)u+X+Y)[(2X+2Y)u+X,\ (2X+2Y)u+X+Y). For integer v≥0v\ge0, the passenger is awake during [(P+Q)v+P, (P+Q)(v+1))[(P+Q)v+P,\ (P+Q)(v+1)).

Find the earliest time when the passenger can get off at BB, or print infinity if this can never happen. When the answer exists, the earliest such time is guaranteed to be an integer.

Input

One line contains four positive integers X,Y,P,QX,Y,P,Q in this order.

Output

Print the earliest integer time when getting off at BB is possible, or exactly infinity if it is impossible.

Subtasks

  • Subtask 1 (20 points): 1≤X,P≤301\le X,P\le30; 1≤Y,Q≤51\le Y,Q\le5; all inputs are integers.
  • Subtask 2 (30 points): 1≤X,P≤1051\le X,P\le10^5; 1≤Y,Q≤501\le Y,Q\le50; all inputs are integers.
  • Subtask 3 (50 points): 1≤X,P≤1091\le X,P\le10^9; 1≤Y,Q≤5001\le Y,Q\le500; all inputs are integers.

Examples

Example 1

Input

5 2 7 6

Output

20

Explanation

The train stops at BB during [5,7)[5,7), [19,21)[19,21), [33,35)[33,35), etc. The passenger is awake during [7,13)[7,13), [20,26)[20,26), [33,39)[33,39), etc. The first overlap is at t=20t=20, which belongs to both [19,21)[19,21) and [20,26)[20,26). At t=5,6,19t=5,6,19 the passenger is asleep, so the earliest time is 2020.

Example 2

Input

1 1 3 1

Output

infinity

Explanation

The train is stopped at BB only at integer times t≡1(mod4)t\equiv1\pmod4, while the passenger is awake only at t≡3(mod4)t\equiv3\pmod4. These sets do not intersect, so getting off is impossible.

Example 3

Input

999999999 1 1000000000 1

Output

1000000000999999999

Explanation

The train period is 20000000002000000000, with its one-second stop at BB beginning at remainder 999999999999999999. The sleep/wake period is 10000000011000000001, with wakefulness at remainder 10000000001000000000. The smallest time meeting both congruences is 10000000009999999991000000000999999999.