#include <bits/stdc++.h>
using namespace std;
const int IM = 1047483647;
unsigned long long cnt1, cnt2;
unsigned long long x, y;
int a_max, b_max;
unsigned long long sum;
vector<unsigned long long> my_vec;
unsigned long long pow(unsigned long long a, int b) {
  unsigned long long t = 1;
  for (int i = 0; i < b; i++) {
    t *= a;
  }
  return t;
}
void build(unsigned long long a, unsigned long long b) {
  a_max = 32;
  b_max = 32;
  for (int i = 1; i < a_max; i++) {
    for (int j = 1; j < b_max; j++) {
      unsigned long long a_part;
      unsigned long long b_part;
      a_part = pow(a, i);
      b_part = pow(b, j);
      if (a_part >= IM) {
        a_max = i;
        continue;
      }
      if (b_part >= IM) {
        b_max = j;
        continue;
      }
      if (a_part * b_part > IM) {
        continue;
      }
      my_vec.push_back(a_part * b_part);
    }
  }
}
unsigned long long to_throw(int value) {
  int a_side = value / x;
  int b_side = value / y;
  int both_side =
      upper_bound(my_vec.begin(), my_vec.end(), value) - my_vec.begin();
  a_side -= both_side;
  b_side -= both_side;
  a_side -= cnt2;
  b_side -= cnt1;
  if (a_side < 0) {
    a_side = 0;
  }
  if (b_side < 0) {
    b_side = 0;
  }
  both_side += a_side + b_side;
  return both_side;
}
int main() {
  cin >> cnt1 >> cnt2 >> x >> y;
  sum = cnt1 + cnt2;
  if (x == y) {
    int peroid;
    peroid = sum / (x - 1);
    sum -= peroid * (x - 1);
    sum += peroid * x;
    if (sum % x == 0) sum--;
    cout << sum;
    return 0;
  }
  build(x, y);
  sort(my_vec.begin(), my_vec.end());
  int value = sum;
  int to_kick = 0;
  while (1) {
    to_kick = to_throw(value);
    if (sum + to_kick == value) {
      cout << value;
      return 0;
    }
    value = sum + to_kick;
  }
}
