#include "bits/stdc++.h" #define int long long using namespace std; using ll = long long; using P = pair; const ll INF = (1LL << 61); ll mod = 998244353; signed main() { ios::sync_with_stdio(false); cin.tie(0); int A, B, N, M; cin >> A >> B >> N >> M; if (A > B) { int cnt = (A - B) / N; A -= cnt * N; B += cnt; cout << min(A, B) << endl; } else { int cnt = (B - A) / M; B -= cnt * M; A += cnt; cout << min(A, B) << endl; } return 0; }