#include <bits/stdc++.h>
using namespace std;
inline bool check(long long x, long long y, long long xMax, long long yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toint(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class tuple<long long, long long, long long> >
inline string tostring(tuple<long long, long long, long long> x) {
  ostringstream sout;
  sout << x;
  return sout.str();
} template <class tuple<long long, long long, long long> >
inline tuple<long long, long long, long long> sqr(
    tuple<long long, long long, long long> x) {
  return x * x;
} template <class tuple<long long, long long, long long> >
inline tuple<long long, long long, long long> mypow(
    tuple<long long, long long, long long> x, long long n) {
  tuple<long long, long long, long long> res = 1;
  while (n > 0) {
    if (n & 1) res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
} inline long long gcd(long long a, long long b) {
  return b ? gcd(b, a % b) : a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const long long dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
                dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const long long mod = 1e9 + 7;
const long long INF = 1e17 + 9;
signed main() {
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  long long a = x, b = n - x;
  while (b) {
    if (a > b) swap(a, b);
    ans += a * 3;
    b -= a;
  }
  cout << ans << endl;
  return 0;
}
