#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; int ans = max(min(A, B), min(A + N, B - 1)); cout << ans << endl; } else { int cnt = (B - A) / M; B -= cnt * M; A += cnt; int ans = max(min(A, B), min(A - 1, B + M)); cout << ans << endl; } return 0; }