#include <bits/stdc++.h>
using namespace std;
long long solve(long long N, long long X) {
  long long ret = X + (N - X) / X * 2 * X;
  long long r = (N - X) % X;
  if (r) ret += X + solve(X, r);
  return ret;
}
int main() {
  long long N, X;
  cin >> N >> X;
  cout << solve(N - X, X) + N << endl;
}
