#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double fRand(double fMin, double fMax) {
  double f = (double)rand() / RAND_MAX;
  return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
long long n, x;
long long Solve(long long a, long long b) {
  if (a < b) swap(a, b);
  if (a % b == 0) return 2 * a - b;
  return 2 * a + Solve(a % b, a % b);
}
int main() {
  cin >> n >> x;
  cout << n + Solve(n - x, x);
  return 0;
}
