function Main() {

  input = require('fs').readFileSync('/dev/stdin', 'utf8');
  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', ans);

}

Main();

function debug() {

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

}
