#include <bits/stdc++.h>
using namespace std;
long long dfs(long long n, long long x) {
  if (n % (n - x) == 0) return n / x * 2 * (n - x);
  return n / (n - x) * 2 * (n - x) + n % (n - x) + dfs(n - x, n % (n - x));
}
int main(void) {
  long long n, x;
  cin >> n >> x;
  cout << dfs(n, x) + x << endl;
  return 0;
}
