import java.util.Scanner;


public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int cnt1 = sc.nextInt();
        int cnt2 = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();
        
        int v;
        int c1, c2;
        for(v=cnt1+cnt2; true; v++)
        {
            c1 = cnt1;
            c2 = cnt2;
            for(int i=0; i<=v; i++)
            {
                if(i%x !=0 && i%y != 0)
                {
                    int temp = Math.max(c1,  c2);
                    if(temp == c1) c1--;
                    else c2 --;
                    continue;
                }
                
                if(c1 != 0)
                {
                    if(i%x != 0)
                        c1--;
                    
                    else if(c2!= 0 && i%y != 0)
                        c2--;
                }
                else
                {
                    if(c2!= 0 && i%y != 0)
                        c2--;
                }
                
                if(c1 == 0 && c2 == 0)
                    break;
            }
            if(c1 == 0 && c2 == 0)
                break;
        }
        
        System.out.println("0");
    }
}
