def extended_gcd(a, b): if a == 0: return b, 0, 1 else: g, x, y = extended_gcd(b % a, a) return g, y - (b // a) * x, x def modinv(a, m): g, x, y = extended_gcd(a, m) if g != 1: raise Exception('逆元が存在しません') else: return x % m def find_minimum_x(A, B, a, b): M = A * B M1 = M // A M2 = M // B M1_inv = modinv(M1, A) M2_inv = modinv(M2, B) x = (a * M2 * M2_inv + b * M1 * M1_inv) % M return x # 入力を取得 A, B, a, b = map(int, input().split()) # 結果を出力 result = find_minimum_x(A, B, a, b) print(result)