#include <bits/stdc++.h>
void move(uint64_t N, uint64_t X, uint64_t *b, uint64_t *d, uint64_t *x,
          uint64_t *y, uint64_t *t) {
  uint64_t ox = *x;
  uint64_t oy = *y;
  switch (*d) {
    case 0:
      b[0] = *x;
      *t += b[1];
      *y = b[1];
      *d = 2;
      break;
    case 1:
      b[1] = *y;
      *t += ((N - X) - b[0]);
      *x = b[0];
      *d = 2;
      break;
    case 2:
      *y = 0;
      *x += oy;
      *d = 0;
      *t += oy;
      if (*x > N - X) {
        *t -= (*x - (N - X));
        *y += (*x - (N - X));
        *x = N - X;
        *d = 1;
      }
      break;
  }
}
int main(void) {
  uint64_t N, X;
  scanf("%d %d", &N, &X);
  uint64_t t = N;
  uint64_t x = 0, y = X;
  uint64_t d = 2;
  uint64_t b[2] = {0, X};
  while (x != N - X || y != 0) {
    move(N, X, b, &d, &x, &y, &t);
  }
  printf("%d\n", t);
  return 0;
}
