A, B, a, b = map(int, input().split()) m0 = (b - a) * pow(A, -1, B) % B x = (m0 * A + a) % (A * B) print(x)