# aとbの最大公約数gcdと、a*x - b*y = 1 の特殊解x, y を返す def extended_gcd(a, b): if a == 0: return b, 0, 1 else: gcd, x, y = extended_gcd(b % a, a) return gcd, y - (b // a) * x, x A, B, a, b = map(int, input().split()) gcd, x, y = extended_gcd(A, B) ans = ((A * x) * (b - a) + a) % (A * B) print(ans)