# 拡張ユークリッドの互除法を用いた解法 from math import gcd # aとbの最大公約数gcdと、a*x - b*y = 1 の特殊解x, y を返す def extended_gcd(a, b): if a % b == 0: gcd = b x = 1 y = 1 - (a // b) else: gcd, pre_x, pre_y = extended_gcd(b, a % b) x = pre_y y = pre_x - (a // b) * pre_y return gcd, x, y A, B, a, b = map(int, input().split()) gcd, x, y = extended_gcd(A, B) # 入力値チェック assert gcd == 1 assert 1 <= A <= 3000 assert 1 <= B <= 3000 assert 0 <= a < A assert 0 <= b < B ans = ((A * x) * (b - a) + a) % (A * B) print(ans)