#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);
  for (int exp = (0); exp < (max1); ++exp) {
    for (int exp2 = (0); exp2 < (max2); ++exp2) {
      com.push_back(pow(x, exp) + pow(y, exp2));
    }
  }
}
bool ok(int v) {
  bool ans = 1;
  if (v - v / y < c2) ans = 0;
  if (v - v / x < c1) ans = 0;
  if (v - (distance(lower_bound(begin(com), end(com), v), com.begin()) + 1) <
      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;
}
