#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = std::vector<T>;
const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
long long int f(long long int a, long long int b) {
  if (a >= b) return (2 * a / b - 1) * b;
  long long int i = (b - a) / a + 1;
  return 2 * a * i + f(a, b - a * i);
}
void solve() {
  long long int n, x;
  cin >> n >> x;
  cout << n + f(x, n - x) << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}
