#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, len;
  while (~scanf("%lld %lld", &n, &x)) {
    int flag = 1;
    if (n == x) {
      len = 0;
      flag = 0;
    }
    if (flag == 1) {
      if (n % 2 == 0) {
        if (n % x == 0) {
          if (x <= n / 2) x = n - x;
          len = 3 * x;
        } else {
          len = (n - 1) * 3;
        }
      } else {
        len = (n - 1) * 3;
      }
    }
    printf("%lld\n", len);
  }
}
