function Main(input) {

  const inputs = input.split('\n');
  const n = inputs[0].split(' ')[0] - 0;
  const x = inputs[0].split(' ')[1] - 0;

  let ans = n;
  let a = Math.max(x, n - x), b = Math.min(x, n - x);
  while (a % b != 0) {
    ans += 2 * Math.floor(a / b) * b;
    [a, b] = [b, a % b];
    while (isNaN(a) || isNaN(b)) ;
  }
  ans += 2 * a - b;

  console.log('%d', ans);

}

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

function debug() {

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

}
