#include <bits/stdc++.h>
int main(void) {
  long long int x;
  long long int n;
  long long int a, a_max, a_min;
  long long int b, b_max, b_min;
  long long int sum = 0;
  int vector = 0;
  scanf("%lld %lld", &n, &x);
  a = x;
  b = x;
  a_max = x;
  a_min = 0;
  b_max = n;
  b_min = x;
  sum += x;
  vector = 2;
  if (x > n - x) {
    x = n - x;
  }
  while (!(a == 0 && b == x)) {
    switch (vector) {
      case 0:
        b_max = b;
        sum += a_max - a;
        a = a_max;
        vector = 1;
        break;
      case 1:
        if (a > b - b_min) {
          vector = 2;
          sum += b - b_min;
          a = a - (b - b_min);
          b = b_min;
        } else {
          vector = 0;
          sum += a;
          b = b - a;
          a = 0;
        }
        break;
      case 2:
        a_max = a;
        sum += b_max - b;
        b = b_max;
        vector = 1;
        break;
      case 3:
        break;
    }
  }
  printf("%lld\n", sum);
  return 0;
}
