#include <bits/stdc++.h>
using ll = long long;
ll answer, N, X;
void solve(int light_direction, ll x, ll y, ll limit_1, ll limit_2, ll limit_3,
           ll limit_4, ll limit_5) {
  if (light_direction == 0) {
    if (limit_3 - y < limit_2 - (x + y)) {
      answer += limit_3 - y;
      solve(2, x, limit_3, limit_1, limit_2, limit_3, x, limit_5);
    } else {
      answer += limit_2 - (x + y);
      solve(1, x, limit_2 - x, x, limit_2, limit_3, limit_4, limit_5);
    }
  } else if (light_direction == 1) {
    answer += x - limit_4;
    solve(2, limit_4, y, limit_1, limit_2, y, limit_4, limit_5);
  } else {
    if (y <= limit_1 - x) {
      answer += y;
      if (x + y == N - X) {
        printf("%lld\n", answer);
        return;
      } else
        solve(0, x + y, 0, limit_1, limit_2, limit_3, limit_4, x + y);
    } else {
      answer += limit_1 - x;
      solve(1, limit_1, x + y - limit_1, limit_1, x + y, limit_3, limit_4,
            limit_5);
    }
  }
}
int main() {
  scanf("%lld %lld", &N, &X);
  solve(0, N - X, 0, N, N, N, 0, 0);
  return 0;
}
