

import java.util.Scanner;

public class main2 {
	
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		

		
		int cnt1 = ni();
		int cnt2 = ni();
		int x = ni();
		int y = ni();
		
		int min = x;
		int minc = cnt1;
		int max = y;
		int maxc = cnt2;
		if (y < x) {
			min = y;
			minc = cnt2;
			
			max = x;
			maxc = cnt1;
		}
		
		long minv1 = howmany(cnt1, x);
		long minv2 = howmany(cnt2, y);
		long v = 0;
		if (minv1 > minv2) {
			v = getmin(minv1, x, cnt2, y);
		} else {
			v = getmin(minv2, y, cnt1, x);
		}
		
		System.out.println(v);
	}
	
	public static long getmin(long minv, int x, int cnt2, int y) {
		cnt2 -= (minv / x) - (minv / lcm(x, y));
		if (cnt2 > 0) {
			long t = minv / y;
			long used = Math.min(cnt2, (t + 1) * y - 1 - minv);
			cnt2 -= used;
			minv+=used;
			if (cnt2 > 0) {
				minv++;
			}
			
			minv += howmany(cnt2, y);
		}
		
		return minv;
	}
	
	public static long howmany(int cnt, int x) {
		long min = cnt / (x - 1);
		cnt -= min * (x - 1);
		if (cnt == 0) {
			return min * x - 1;
		} else {
			return min * x + cnt;
		}
	}
	
	public static long gcd(long a, long b) {
		while (b != 0) {
			long t = b;
			b = a % b;
			a = t;
		}

		return a;
	}
	public static long lcm(long a, long b) {
		return (a * b) / gcd(a, b);
	}
	private static int ni() {
		return sc.nextInt();
	}
	
	private static long nl() {
		return sc.nextLong();
	}
	
	private static String ns() {
		return sc.next();
	}
	
	private static String line() {
		return sc.nextLine();
	}
}
