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