import java.util.Scanner;


public class projectB {

	private static Scanner sc;
    public static void main(String[] args) 
    {
        sc = new Scanner(System.in);
        int cnt1, cnt2, x,y;
        cnt1=sc.nextInt();
        cnt2=sc.nextInt();
        x=sc.nextInt();
        y=sc.nextInt();
        int k1=pr(cnt1, cnt2, x, y), k2=pr(cnt2, cnt1,y, x);
        if(k1<k2)
        System.out.println(k1);
        else
        	System.out.println(k2);

	}
    public static int pr(int cnt1, int cnt2, int x, int y)
    {
    	 int k1=0, k2=0, i=1;
    	 while(cnt1!=k1 || cnt2!=k2)
         {
         	if(i%x!=0 && k1!=cnt1)
         		k1++;
         	else if (i%y!=0 && k2!=cnt2)
         		k2++;
         	i++;
         }
    	 return i-1;
    }

}


