#include <bits/stdc++.h>
using namespace std;
const int N = 2000100;
const int mod = (int)pow(10, 9) + 7;
const long long INF = 1LL << 60;
long long n, x, r, u, d, ans, ct;
pair<long long, long long> p;
int main() {
  cin >> n >> x;
  r = x;
  u = x;
  ans += n;
  p = pair<long long, long long>{x, n};
  while (p != pair<long long, long long>{0, x}) {
    if (ct % 2 == 0) {
      ans += min(p.first, p.second - u);
      if (p.first < p.second - u)
        p = pair<long long, long long>{0, p.second - p.first};
      else
        p = pair<long long, long long>{p.first - (p.second - u), u};
    } else {
      if (p.first == 0)
        ans += r, p = pair<long long, long long>{r, p.second}, d = p.second;
      if (p.second == u)
        ans += d - u, p = pair<long long, long long>{p.first, d};
    }
    ++ct;
  }
  cout << ans << endl;
  return 0;
}
