#include <bits/stdc++.h>
using namespace std;
const double Pi = acos(-1.0);
const int INF = 1000000000;
long long F(long long N, long long x) {
  long long cnt = N / x;
  if (cnt * x == N) return x * 2 * (cnt - 1);
  long long res = cnt * x * 2;
  res += F(x, N % x);
  return res;
}
int main() {
  long long n, x;
  cin >> n >> x;
  long long res = x + F(n, x);
  cout << res << endl;
}
