 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) {

  inputs = input.split('\n');

  let N, X;
  [N, X] = inputs[0].split(' ').map((e) => e - 0);

  /*let ans = N;
  let a = X, b = N - X;
  while (a % b != 0) {
    ans += 2 * Math.floor(a / b) * b;
    [a, b] = [b, a % b];
  }
  ans += 2 * a - b;*/

  console.log('%d', 3*(N - gcd(N - X, X));

}

Main(require('fs').readFileSync('/dev/stdin', 'utf8'));

function debug() {

  let input = document.getElementById('input').value;
  Main(input);

}
