#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
long long f(int a, int b) {
  if (b < a) std::swap(a, b);
  if (a == 1) return 2 * b - 1;
  if (a) return 2 * a * (b / a) + f(b % a, a);
  return 0;
}
int main() {
  int n, x;
  cin >> n >> x;
  cout << n + f(x, n - x) << endl;
  return 0;
}
