#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const int INF = INT_MAX / 3;
const long long LINF = LLONG_MAX / 3LL;
long long mod(long long a, long long m) { return (a % m + m) % m; }
int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
    dy[9] = {1, 0, -1, 0, 1, -1, 1, -1, 0};
long long rec(long long a, long long b) {
  if (a == b) return a;
  if (a < b) return rec(b, a);
  long long t = rec(a - b, b);
  return b * 2 + t;
}
int main() {
  long long n, x;
  long long ans = 0;
  cin >> n >> x;
  ans += x;
  ans += n - x;
  ans += rec(n - x, x);
  cout << ans << endl;
  return 0;
}
