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