#include <bits/stdc++.h>
using namespace std;
long long sum = 0;
void ippan(long long N_x, long long p) {
  long long G = N_x - p;
  sum += G;
  long long kai = G / p;
  long long Gp = G % p;
  if (Gp) {
    sum += 2 * kai * p;
    sum += p - Gp;
    ippan(max(p, Gp), min(Gp, p));
  } else {
    sum += (2 * kai - 1) * p;
    return;
  }
}
void hajime(long long N, long long X) {
  long long N_x = N - X;
  X = max(X, N_x);
  N_x = N - X;
  sum += X;
  long long kai = X / (N_x);
  sum += 2 * kai * (N_x);
  long long p = X % N_x;
  if (p) {
    sum += N_x;
    sum += p;
    ippan(N_x, p);
  } else {
    return;
  }
}
int main() {
  long long N, X;
  cin >> N >> X;
  hajime(N, X);
  cout << sum << endl;
  return 0;
}
