#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = MOD + 1;
const long double EPS = 1e-12;
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
long long solve(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == 0) return 0;
  return a * 2 * (b / a) + solve(a, b % a);
}
int main() {
  cin.sync_with_stdio(false);
  long long n, x;
  cin >> n >> x;
  cout << solve(n - x, x) + n - 1 << endl;
  return 0;
}
