#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X;
  cin >> N >> X;
  auto sum = X;
  if (N == X * 2) {
    sum += 2 * X;
  } else {
    sum += (N / (N - X) * 2 - 1) * (N - X);
    auto l = N - X;
    auto w = N % (N - X);
    while (true) {
      if (l % w == 0) {
        sum += (l / w * 2 - 1) * w;
        break;
      }
      sum += (l / w) * 2 * w;
      auto prevW = w;
      w = l % w;
      l = prevW;
    }
  }
  cout << sum << endl;
  return 0;
}
