#include <bits/stdc++.h>
using namespace std;
long long int Solve(long long int N, long long int X) {
  if (!N || !X) return 0;
  if (2 * X <= N) {
    return 3 * X + Solve(N - X, N - 2 * X);
  } else {
    long long int C = X / (N - X);
    return X + 2 * (N - X) * (2 * C + 1) - N +
           Solve(N - (N - X) * C, N - (N - X) * (C + 1));
  }
}
void Stress() {
  static const long long int NMAX = 1000000000000LL;
  static const double MAGIC = (3 - sqrt(5)) * 0.5;
  long long int N = NMAX;
  long long int X = N * MAGIC;
  cout << Solve(N, X) << '\n';
}
void Submit() {
  long long int N, X;
  cin >> N >> X;
  cout << Solve(N, X) << '\n';
}
int main() {
  Submit();
  return 0;
}
