from math import gcd def floor_sum(n, m, a, b): ans = 0 while True: if a >= m or a < 0: ans += n * (n - 1) * (a // m) // 2 a %= m if b >= m or b < 0: ans += n * (b // m) b %= m y_max = a * n + b if y_max < m: break n, b, m, a = y_max // m, y_max % m, a, m return ans from math import gcd class Fraction: def __init__(self, top, bottom): GCD = gcd(top, bottom) if bottom < 0: top = -top bottom = -bottom self.top = top//GCD self.bottom = bottom//GCD def to_fraction(n): if type(n) == int: return Fraction(n, 1) else: return n def compare(L, R): L, R = to_fraction(L), to_fraction(R) a = L.top*R.bottom b = L.bottom*R.top if a < b: return -1 elif a == b: return 0 else: return 1 def max_f(L, R): return L if compare(L, R) == 1 else R def min_f(L, R): return L if compare(L, R) == -1 else R def ADD(L, R): L, R = to_fraction(L), to_fraction(R) l, r = L.bottom, R.bottom lt = L.top*r rt = R.top*l top = lt+rt bottom = l*r GCD = gcd(top, bottom) top //= GCD bottom //= GCD if bottom < 0: top = -top bottom = -bottom return Fraction(top, bottom) def SUB(L, R): L, R = to_fraction(L), to_fraction(R) l, r = L.bottom, R.bottom lt = L.top*r rt = R.top*l top = lt-rt bottom = l*r GCD = gcd(top, bottom) top //= GCD bottom //= GCD if bottom < 0: top = -top bottom = -bottom return Fraction(top, bottom) def MUL(L, R): L, R = to_fraction(L), to_fraction(R) top = L.top*R.top bottom = L.bottom*R.bottom GCD = gcd(top, bottom) top //= GCD bottom //= GCD if bottom < 0: top = -top bottom = -bottom return Fraction(top, bottom) def DIV(L, R): L, R = to_fraction(L), to_fraction(R) top = L.top*R.bottom bottom = L.bottom*R.top GCD = gcd(top, bottom) top //= GCD bottom //= GCD if bottom < 0: top = -top bottom = -bottom return Fraction(top, bottom) A, B, C, D = map(int, input().split()) if compare(Fraction(A, B), Fraction(C, D)) == 0: exit(print(-1)) if compare(Fraction(A, B), Fraction(C, D)) == 1: A, B, C, D = C, D, A, B left = 0 right = 10**12 while left+1 < right: mid = (left+right)//2 if compare(ADD(Fraction(A*mid, B), 1), Fraction(C*mid, D)) == 1: left = mid else: right = mid print(left-(floor_sum(left, D, C, C+D//2)-floor_sum(left, B, A, A+B//2)))