#include <bits/stdc++.h>
template <typename T>
void Scan(T& t) {
  std::cin >> t;
}
template <typename First, typename... Rest>
void Scan(First& first, Rest&... rest) {
  std::cin >> first;
  Scan(rest...);
}
using ll = long long;
constexpr ll INFINT = 1 << 30;
constexpr ll INFLL = 1LL << 60;
constexpr double EPS = 0.0000001;
constexpr ll MOD = 1000000007;
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ;
  ll n, x;
  Scan(n, x);
  ;
  ll ans = n;
  ll a = x, b = n - x;
  while (true) {
    if (std::fabs(1.0 * b / a - b / a) < EPS) {
      ans += b / a * 2 * a - a;
      break;
    } else {
      ans += b / a * 2 * a;
      ll t = b;
      b = a;
      a = t - (t / a) * a;
    }
  }
  std::cout << ans;
  std::cout << "\n";
  ;
  return 0;
}
