#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 * (long long)ceil((double)(n - x) / x) * x);
  else
    printf("%lld", 3LL * (long long)ceil((double)x / (n - x)) * (n - x));
  return 0;
}
