#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, x;
  cin >> n >> x;
  ll ans = 0;
  ans += (n - x) + 3 * x;
  ll bd = n - 2 * x;
  if (x >= bd) {
    ans += (2 * x / bd - 1) * bd;
  } else {
    ans += (2 * bd / x - 1) * x;
  }
  cout << ans << endl;
}
