#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<math.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
long long a, b, x, y, E, T, d, mx, TT;
int main()
{
    scanf("%lld%lld%lld%lld", &a, &b, &x, &y);
    E=1; T=4000000000;
    while(E+1<T)
    {
        d=E+(T-E)/2;
        if(d-d/x>a) T=d;
        else E=d;
    }
    mx=T-1;
    if(mx%x==0) mx--;
    TT=x*y/(__gcd(x, y));
    b=b-mx/x+b/TT;
    if(b<1)
    {
        printf("%lld", mx);
        return 0;
    }
    E=mx+1; T=4000000000;
    while(E+1<T)
    {
        d=E+(T-E)/2;
        if(d-d/y+mx/y>b) T=d;
        else E=d;
    }
    T--;
    if(T%y==0) T--;
    printf("%lld", T);
    return 0;
}
