#include <bits/stdc++.h>
int main() {
  long long N, X;
  scanf("%lld %lld", &N, &X);
  long long n, x;
  n = (((X) > (N - X)) ? (X) : (N - X));
  x = (((X) < (N - X)) ? (X) : (N - X));
  long long c = 0;
  while (n != 0) {
    if (n < x) {
      c += 3 * n;
      x -= n;
    } else {
      c += 3 * x;
      n -= x;
    }
  }
  printf("%lld\n", c);
  return 0;
}
