#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};
unsigned long long sttoi(std::string str) {
  unsigned long long ret;
  std::stringstream ss;
  ss << str;
  ss >> ret;
  return ret;
}
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  if (b > a) swap(a, b);
  if (b == 0) return a;
  return gcd(b, a % b);
}
int comb(int n, int k) {
  int ans = 1;
  for (int i = 0; i < min(k, n - k); i++) {
    ans *= n - i;
    ans /= i + 1;
  }
  return ans;
}
long long mod_pow(long long n, long long p, long long mod) {
  if (p == 0) return 1 % mod;
  if (p < 2) {
    return n % mod;
  } else {
    long long d = 2, value = (n % mod) * (n % mod) % mod;
    while (d * 2 < p) {
      d *= 2;
      value *= value;
      value %= mod;
    }
    value %= mod;
    return ((value % mod) * (mod_pow(n, p - d, mod) % mod)) % mod;
  }
}
bool isPrime(int n) {
  if (n <= 1) return false;
  if (n == 2) return true;
  if (n % 2 == 0) return false;
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return false;
  }
  return true;
}
void solve() {
  long long n, x, ans;
  cin >> n >> x;
  if (n % x == 0) {
    if (x == n / 2) {
      ans = 3 * x;
    } else {
      ans = x * 3 * ((n / x) - 1);
    }
  } else if (x <= n / 2) {
    ans = 3 * x + (int)((n - x - 1) / x) * 3 * x;
  } else {
    ans = 3 * (n - x) + ((x - 1) / (n - x)) * 3 * (n - x);
  }
  cout << (ans) << endl;
  ;
}
int main() {
  solve();
  return 0;
}
