#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline T read() {
  static T _, __;
  static char c_;
  for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar())
    if (c_ == '-') __ = -1;
  for (; c_ >= '0' && c_ <= '9'; c_ = getchar())
    _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
  return _ * __;
}
long long f(long long x, long long N) {
  if (x > N) swap(x, N);
  if (N % x == 0) return 2ll * N - x;
  return 2ll * x * (N / x - 1) + f(N % x, x);
}
int main() {
  long long N = read<long long>(), x = read<long long>();
  printf("%lld\n", N + f(x, N));
  return 0;
}
