#include <bits/stdc++.h>
using namespace std;
vector<int> com;
int maxv = 2e9;
int c1, c2, x, y;
void CM(int x, int y) {
  int max1 = (double)log(maxv) / (double)log(x);
  int max2 = (double)log(maxv) / (double)log(y);
  long long m;
  for (int exp = (1); exp < (max1 + 1); ++exp) {
    for (int exp2 = (1); exp2 < (max2 + 1); ++exp2) {
      m = (long long)pow(x, exp) * (long long)pow(y, exp2);
      if (m < (long long)maxv) com.push_back(m);
    }
  }
  sort(begin(com), end(com));
}
bool ok(int v) {
  bool ans = 1;
  if (v - v / x < c1) ans = 0;
  if (v - v / y < c2) ans = 0;
  if (v - v / (x * y) < c1 + c2) ans = 0;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> c1 >> c2 >> x >> y;
  CM(x, y);
  long long lef = 0, rig = maxv;
  long long mid;
  while (lef <= rig) {
    mid = (lef + rig) / 2;
    if (ok(mid))
      rig = mid - 1;
    else
      lef = mid + 1;
  }
  cout << rig + 1 << '\n';
  return 0;
}
