#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
using ll = long long;
ll Rec(ll N, ll X) {
  if (N == 2 * X) return 3 * X;
  if (N > 2 * X) return 3 * X + Rec(N - X, N - 2 * X);
  ll K = X / (N - X);
  ll R = X % (N - X);
  ll ANS = 2 * K * (N - X);
  if (R == 0) return ANS;
  return ANS + X + 2 * R + Rec(N - X, N - X - R);
}
void Solve() {
  ll N, X;
  scanf("%lld%lld", &N, &X);
  printf("%lld", Rec(N, X));
}
int main() {
  Solve();
  return 0;
}
