#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
constexpr ll inf = static_cast<ll>(1e17);
constexpr int iinf = static_cast<int>(1e9);
constexpr ll mod = static_cast<ll>(1e9 + 7);
constexpr long long MOD(long long in) { return in % mod; }
ll n, x;
int main() {
  std::cin >> n >> x;
  ll ans = n;
  ll up = n - x, right = x;
  while (up != 0 && right != 0) {
    ans += right / up;
    auto nr = right % up;
    auto nu = up - nr;
    ans += nr * 2;
    if (nr == 0 && right / up != 1) ans += up;
    right = nr;
    up = nu;
  }
  std::cout << ans << std::endl;
  return 0;
}
