#include <bits/stdc++.h>
using namespace std;
long long i, j = 0, k = 0, l = 0, m = 0, n, t = 0, sum = 0, r = 0, q = 0, p = 0,
             z = 0;
int a, b;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> a >> b;
read:
  z = 0;
  p = a - 1;
  j = n / p;
  k = n % p;
  j = j * a - 1;
  if (k) j = j + k + 1;
  t = a;
  while (t <= j && z < m) {
    if (t % b) z++;
    t = t * a;
  }
  while (z < m) {
    j++;
    if (j % b > 0) z++;
  }
  if (r == 0) {
    l = j;
    swap(n, m);
    swap(a, b);
    r++;
    goto read;
  }
  cout << min(l, j);
  return 0;
}
