#include <bits/stdc++.h>
using namespace std;
inline int64_t gcd(const int64_t x, const int64_t y) {
  return (y != 0LL) ? gcd(y, x % y) : x;
}
inline int64_t lcm(const int64_t x, const int64_t y) {
  return x * y / gcd(x, y);
}
inline int64_t sum(const int64_t x, const int64_t k) { return x - x / k; }
inline int64_t find(const int64_t n, const int64_t k) {
  int64_t lef = 1LL, rig = 0x7fffffffffffffff;
  while (lef <= rig) {
    int64_t mid = (lef + rig) >> 1;
    if (sum(mid, k) >= n) {
      if (sum(mid - 1LL, k) < n) return mid;
      rig = mid - 1;
    } else
      lef = mid + 1;
  }
  exit(1);
}
inline void updmax(int64_t &x, const int64_t y) {
  if (x < y) x = y;
}
int main() {
  int64_t n, m, x, y;
  while (cin >> n >> m >> x >> y) {
    int64_t ans = find(n, x);
    m += ans / x + ans / y - ans / lcm(x, y);
    updmax(ans, find(m, y));
    cout << ans << endl;
  }
  return 0;
}
