#include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T a, T b) {
  for (; a != b; ++a) cerr << *a << ' ';
  cerr << endl;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
namespace std {
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
}  // namespace std
long long int n;
long long int x0;
long long int rec(long long int x, long long int y) {
  long long int L = min(n - x - y, x);
  if (y == n - x0 || x == 0) return 0;
  long long int res = 0;
  if (L == x) {
    long long int dif = n - x - y - x;
    long long int rep = max(1ll, dif / L);
    chmin(rep, (n - x0 - y) / L);
    res += L * 3 * rep;
    res += rec(x, y + L * rep);
  } else {
    long long int dif = x - (n - x - y);
    long long int rep = max(1ll, dif / L);
    chmin(rep, x / L);
    res += L * 3 * rep;
    res += rec(x - L * rep, y);
  }
  return res;
}
int main() {
  cin >> n >> x0;
  long long int res = rec(x0, 0);
  cout << res << endl;
  return 0;
}
