#include <bits/stdc++.h>
using namespace std;
using ui = unsigned;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
const ll mod = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[]{0, -1, 0, 1, -1, -1, 1, 1};
template <class T, class U>
void chmin(T &x, const U &y) {
  x = min<T>(x, y);
}
template <class T, class U>
void chmax(T &x, const U &y) {
  x = max<T>(x, y);
}
struct prepare_ {
  prepare_() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
  }
} prepare__;
ll in() {
  ll x;
  cin >> x;
  return x;
}
vector<ll> in(size_t n) {
  vector<ll> v(n);
  for (auto &&x : v) cin >> x;
  return v;
}
int main() {
  ll n, x;
  cin >> n >> x;
  ll s = n - 1;
  ll a = n - x, b = x;
  while (b > 0) {
    ll t = a / b;
    s += b * t * 2;
    a -= b * t;
    swap(a, b);
  }
  cout << s << endl;
}
