#include <bits/stdc++.h>
using namespace std;

#define     ll      long long int

ll x, y, c1, c2, g;

int chk(ll c)
{
    ll x1, y1, g1, x2, y2, baki;
    if (x == y)
    {
        x1 = c / x;
        baki = c - x1;
        if (baki >= (c1 + c2))  return 1;
        return 0;
    }
    if (g == 1)
    {
        x1 = c / x;
        y1 = c / y;
        baki = c - (x1 + y1);
        x2 = c1 - y1;
        y2 = c2 - x1;
        //cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
        if (x2 <= 0 && y2 <= 0)     return 1;
        if (x2 < 0) x2 = 0;
        if (y2 < 0) y2 = 0;
        if (baki >= (x2 + y2))      return 1;
        return 0;
    }
    else
    {
        g1 = c / g;
        x1 = c / x;
        y1 = c / y;
        baki = c - ((x1 + y1) - g1);
        x2 = c1 - (y1 - g1);
        y2 = c2 - (x1 - g1);
        if (x2 <= 0 && y2 <= 0)     return 1;
        if (x2 < 0) x2 = 0;
        if (y2 < 0) y2 = 0;
        if (baki >= (x2 + y2))      return 1;
        return 0;
    }
}

ll bin_( )
{
    ll lo, hi, mid, fl, ans = -1;
    lo = 1;
    hi = 1e18;
    while (lo <= hi)
    {
        mid = lo + (hi - lo) / 2;
        fl = chk(mid);
        if (fl == 1)
        {
            hi = mid - 1;
            ans = mid;
        }
        else
        {
            lo = mid + 1;
        }
    }
    return ans;
}

int main( )
{
    cin >> c1 >> c2 >> x >> y;
    g = __gcd(x, y);
    cout << bin_( ) << "\n";
    return 0;
}
