시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 1024 MB26111157.895%

문제

You have a new job. You can honestly say that coffee is not your cup of tea. You have many customers that ask for a particular ratio of coffee to milk. You don't have the ability to make arbitrary measurements. You have two cups. Both cups are the same size. You have a machine that dispenses pure coffee, and another machine that dispenses pure milk.

You can completely fill one empty cup with coffee and the second empty cup with milk. You can also take these two full cups and create a mix such that the result will have exactly half of each cup, i.e., half coffee and half milk. So, you have created a mix (a cup) that is half coffee and half milk but, unfortunately, you have wasted a full cup of ingredients (the second half of coffee and the second half of milk).

Now that you have a cup that is half coffee and half milk, you can fill the second cup with coffee or fill the second cup with milk. Then, you can mix your two cups to create a cup with a different coffee-to-milk ratio. Note that, when mixing two cups, you use exactly half of each cup and waste exactly half of each cup.

By repeating the above process, you can make some coffee-to-milk ratios but some ratios are literally impossible.

Given the tolerance range a customer has for their coffee-to-milk ratio, determine the least amount of ingredients you will waste assuming you prepare the customer's order optimally.

입력

There is only one input line; it contains four integers: C1, M1, C2, and M2 (0 ≤ C1, M1, C2, M2 ≤ 106) representing the coffee-to-milk ratio range the customer accepts. More specifically, the ratio can be anywhere from C1 coffee parts per M1 milk parts to C2 coffee parts per M2 milk parts. You are guaranteed that:

  • The two ratios will not be identical.
  • C1 + M1 > 0
  • C2 + M2 > 0
  • C1 / (C1 + M1) ≠ C2 / (C2 + M2)

출력

Print one integer representing the least number of cups of ingredients wasted assuming you prepare the customer’s order optimally.

예제 입력 1

1 2 2 1

예제 출력 1

1

예제 입력 2

1 10 0 1

예제 출력 2

0

예제 입력 3

1 99 49 51

예제 출력 3

2