

import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range;
import std.bigint;

void main(){
	
	long[] ins = readln.chomp.split.map!(to!long).array;
	long n = ins[0], x = ins[1];
	
	long y = n - x;
	
	long a, b;
	if(x > y) a = x, b = y;
	else a = y, b = x;
	
	long ans = a + b;
	while(b > 0){
		long r = a % b;
		ans += b * 2 * (a / b);
		a = b;
		b = r;
		}
	ans -= a;
	
	ans.writeln;
	
	}