#include <bits/stdc++.h>
using namespace std;
template <class T>
using vv = vector<vector<T>>;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "{";
  for (int(i) = 0; (i) < (t.size()); ++(i)) {
    os << t[i] << ",";
  }
  os << "}" << endl;
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const long long MOD = 1e9 + 7;
long long fun(long long n, long long x) {
  long long a = x, b = n - x;
  if (!a || !b) return 0;
  if (a > b) {
    return a + b + a / b * 2 * b + a % b + fun(b, b - a % b) - (b - a % b);
  } else {
    return a + b + b / a * 2 * a + fun(b, b % a) - a - b % a;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  long long n, x;
  cin >> n >> x;
  cout << fun(n, x) << endl;
  return 0;
}
