#include <bits/stdc++.h>
using ull = unsigned long long int;
using namespace std;
int main() {
  ull N, X, next_N, next_X;
  cin >> N >> X;
  X = min(X, N - X);
  ull ans = 0;
  for (;;) {
    if (N % X == 0) {
      ans += ((N / X) * 2 - 1) * X;
      break;
    } else {
      ans += N;
      ans += ((N - X) / X) * X * 2;
    }
    next_N = X;
    next_X = N % X;
    N = next_N;
    X = next_X;
  }
  cout << ans << endl;
  return 0;
}
