#include <bits/stdc++.h>
using namespace std;
const int DX[4] = {0, 1, 0, -1}, DY[4] = {-1, 0, 1, 0};
const int INF = 1e9;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
long long rec(long long a, long long b) {
  if (a == 1 && b == 1) return 1;
  return (b / a) * a * 2 + rec(a, b % a);
}
int main(int argc, char const *argv[]) {
  long long N, X;
  cin >> N >> X;
  if (X * 2 == N) {
    cout << X * 3 << endl;
    return 0;
  }
  long long a = X, b = N - X;
  cout << N + rec(N - X, X) << endl;
  return 0;
}
