import pypyjit pypyjit.set_param('max_unroll_recursion=-1') 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) ans = ((A * x) * (b - a) + a) % (A * B) print(ans)