#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef unsigned long long Long;

Long f(Long n, Long x) {
  if (n == x) return x;
  int k = (n + x - 1) / x - 1;
  return 2 * x * k + f(x, n - k * x);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  Long n, x;
  cin >> n >> x;

  cout << n + f(n - x, x);

}

