
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int x = Integer.parseInt(s[1]);
		int result = (n-gcd(n,n-x))*3;
		System.out.println(result);
		
	}
	public static int gcd(int a, int b){
	    if(b==0) return a;
	    return gcd(b, a%b);
	}
}
