#include <bits/stdc++.h>
using namespace std;
long long n;
long long x;
int main() {
  scanf("%lld%lld", &n, &x);
  long long ans = n;
  long long at = x;
  long long kan = n - x;
  while (x != 0) {
    long long tmp = kan / x;
    ans += tmp * 2LL * x;
    kan %= x;
    x = kan;
    if (x == 0) break;
    tmp = at / x;
    ans += tmp * 2LL * x;
    at %= x;
    ans -= x;
    x = at;
  }
  printf("%lld\n", ans);
  return 0;
}
