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