const lines = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n');

let N, X;
[N, X] = lines[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);
