#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  int t;
  while (b != 0) {
    t = a;
    a = b;
    b = t % b;
  }
  return a;
}
int main() {
  long long int kk, max, n = 0, m = 0, state = 0, a, b, c, d, i, j, k;
  cin >> a >> b >> c >> d;
  i = gcd(c, d);
  i = c * d / i;
  kk = a / (c - 1);
  if (kk > 0)
    n = kk * c - 1;
  else
    kk = 0;
  n += a - kk;
  kk = b / (d - 1);
  if (kk > 0)
    m = kk * d - 1;
  else
    kk = 0;
  m += b - kk;
  if (n > m) {
    k = n / i;
    max = n;
  } else {
    k = m / i;
    max = m;
  }
  if (c > d) {
    kk = k / (c - 1) - 1;
    if (kk > 0)
      max += kk * c - 1;
    else
      kk = 0;
    max += k - kk;
  } else {
    kk = k / (d - 1) - 1;
    if (kk > 0)
      max += kk * d - 1;
    else
      kk = 0;
    max += k - kk;
  }
  cout << max << endl;
  return 0;
}
