#include using namespace std; int main() { int A, B, a, b; cin >> A >> B >> a >> b; int ans = a; int i = 0; while (true) { ans = A * i + a; if (ans % B == b) break; i++; } cout << ans << endl; }