function gcd(a, b) {
	while (true) {
		if (a > b) {
			a = a % b;
			if (a === 0) return b;
		} else {
			b = b % a;
			if (b === 0) return a;
		}
	}
}

function Main(input) {
const lines = input;
const NS = lines[0].split(' ');
let N = Number(NS[0]);
let S = Number(NS[1]);
 
console.log(3 * (N - gcd(N - S, S)));
}
 
Main(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n'));
 
