#include <bits/stdc++.h>
int gcd(int a, int b) {
  if (!a || !b) return a + b;
  return gcd(a % b, b % a);
}
int cnt1, cnt2, x, y, cnt, xy;
int main() {
  while (scanf("%d %d %d %d", &cnt1, &cnt2, &x, &y) != EOF) {
    cnt = cnt1 + cnt2;
    xy = x * y / gcd(x, y);
    int s = 0, ans;
    while (cnt >= xy) {
      s += (int(cnt / xy) * xy);
      cnt = cnt % xy + cnt / xy;
    }
    s += cnt;
    ans = s;
    s = 0;
    while (cnt1 >= x) {
      s += (int(cnt1 / x) * x);
      cnt1 = cnt1 % x + cnt1 / x;
    }
    s += cnt1;
    if (ans < s) ans = s;
    s = 0;
    while (cnt2 >= y) {
      s += (int(cnt2 / y) * y);
      cnt2 = cnt2 % y + cnt2 / y;
    }
    s += cnt2;
    if (ans < s) ans = s;
    printf("%d\n", ans);
  }
  return 0;
}
