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