#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<long long>;
using pll = pair<long long, long long>;
ll myrec(ll N, ll X) {
  if (X == 0) return 0;
  if (2 * X == N) return 3 * X;
  if (2 * X < N) {
    ll Nnew = N % X + X;
    ll d = myrec(Nnew, X);
    return d + 3 * (N - Nnew);
  }
  if (2 * X > N) {
    ll Nnew = N - X + X % (N - X);
    ll d = myrec(Nnew, X % (N - X));
    return d + 3 * (N - Nnew);
  }
}
void solve(long long N, long long X) {
  ll ans = myrec(N, X);
  cout << ans << endl;
}
int main() {
  long long N;
  scanf("%lld", &N);
  long long X;
  scanf("%lld", &X);
  solve(N, X);
  return 0;
}
