///*****************************************************///
///*.......................Faiyaj Bin Amin............................*///
///*...................................................................*///
///*.........................KUET CSE 2K17......................*///
///*...................................................*///
///*****************************************************///

#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define fastio      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define ret return
#define rep(q, i, n) for(int q=i; q<n; q++)
#define forn(i, n) for(int i=0; i<n; i++)
#define mems(a,x) memset(a,x,sizeof(a))
#define ll long long
#define pb push_back
#define em emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
#define pii pair<int,int>
#define pdd pair<double, double>

#define see(x)        cerr<< __LINE__ << ": " << (#x) << " is "<< (x) <<'\n';
#define seecontent(v) for(auto x : v) { cout << x << " ";} cout << "\n\n";

#define PI 2*acos(0.0)
#define lcm(a, b)  ((a / __gcd(a, b) ) * b)

#define filein freopen ("input.txt", "r", stdin)
#define fileout freopen ("output.txt", "w", stdout)

#define mx 100005
#define INF 1e18
#define MOD 1000000007
#define MIN 0

//template <typename T>  using orderedSet =
//    tree<T, null_type, less_equal<T>,
//    rb_tree_tag, tree_order_statistics_node_update>; //find_by_order, order_of_key


/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0}; // Directional
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

/**------------------------------------BITMASK------------------------------------------**/
//int Change(int N, int pos){return N= N^(1<<pos);}
//bool Check(int N, int pos){return (bool)(N &(1<<pos));}  /// check bit 0 or 1
/**-----------------------------------------------------------------------------------------------**/

//using namespace __gnu_pbds;
using namespace std;

int bs(int req, int z)
{
    int b=1, e=1e9, ans=-1;
    while(b<=e)
    {
        int mid=(b+e)/2;

        int div=mid/z;
        if(mid-div>=req)
        {
            ans=mid;
            e=mid-1;
        }
        else
            b=mid+1;
    }
    return ans;
}


int bs2(int req, int z, int b)
{
    int e=1e9, ans=-1, usel=b;
    while(b<=e)
    {
        int mid=(b+e)/2;

        int div=mid/z-usel/z;
        if(mid-div-usel>=req)
        {
            ans=mid;
            e=mid-1;
        }
        else
            b=mid+1;
    }
    return ans;
}

int main()
{
    fastio;
    int c,d,x,y;
    cin>>c>>d>>x>>y;

    int first = bs(c, x);

    int l = lcm(x,y);

    int cov = (first/x)-(first/l);

    int sec=1e9+5, mini;
    if(cov>=d)
    {
        mini=first;
    }
    else
    {
        sec=bs2(d-cov, y, first);
        mini=sec;
    }
////
////    see(cov);
////    see(first);
////    see(sec);
////    see(mini);
    first = bs(d, y);
    cov = (first/y)-(first/l);

    if(cov>=x)
    {
        mini=min(mini, first);
    }
    else
    {
        sec=bs2(c-cov, x, first);
        mini=min(mini, sec);
    }

////    see(cov);
////    see(first);
////    see(sec);
    cout << mini <<"\n";

    return 0;
}
