#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 405;
const long long mod = 1000000007;
const double PI = acos(-1.0);
long long n, x;
long long cal(long long n, long long x) {
  if (n <= 0 || x <= 0) {
    return 0;
  }
  if (x % (n - x) == 0 && x * 2LL >= n) {
    return 3LL * x;
  } else if ((n - x) % x == 0 && x * 2LL <= n) {
    return 3LL * (n - x);
  } else {
    if (x < n - x) {
      return 3LL * x + cal(n % x, x);
    } else {
      return 3LL * (n - x) + cal(x % (n - x), n - x);
    }
  }
}
void solve() { printf("%lld\n", cal(n, x)); }
int main() {
  while (cin >> n >> x) {
    solve();
  }
  return 0;
}
