#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline void chkmax(T &x, const T2 &y) {
  if (x < y) x = y;
}
template <class T, class T2>
inline void chkmin(T &x, const T2 &y) {
  if (x > y) x = y;
}
const int MAXN = (1 << 20);
int64_t n, x;
void read() { cin >> n >> x; }
int64_t rec(int64_t a, int64_t b) {
  if (a == 0 || b == 0) return 0;
  if (a > b) return rec(b, a);
  int64_t c = (b / a);
  return rec(a, b - c * a) + c * a * 2ll;
}
void solve() { cout << n + rec(x, n - x) - 1ll << '\n'; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  read();
  solve();
  return 0;
}
