#include <bits/stdc++.h>
int main(void) {
  unsigned long long n, x;
  (void)scanf("%llu%llu", &n, &x);
  if (n - x <= x) {
    printf("%llu\n", x * 3);
  } else if (x < n - x) {
    unsigned long long int res = n;
    res += (n - x) * 2 - x;
    if ((n - x) % x != 0) {
      res += x * 2 - ((n - x) % x);
    }
    printf("%llu\n", res);
  }
  return (0);
}
