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