#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, ans = 0;
  scanf("%lld", &n);
  scanf("%lld", &x);
  int ikkk;
  ans = n;
  long long t = x, p, q = n - x;
  while (t) {
    p = q / t;
    ans += (p * t * 2);
    p = q;
    q = t;
    ikkk = t;
    t = p % t;
  }
  ans = ans - ikkk;
  printf("%lld\n", ans);
  return 0;
}
