#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  if (n % 2 == 0 && x == x / 2) {
    cout << 3 * x << endl;
  } else if (x <= n / 2) {
    cout << 3 * (x * ((n - x) / x) + (n - x) % x * x) << endl;
  } else {
    int y = n - x;
    cout << 3 * (y * ((n - y) / y) + (n - y) % y * y) << endl;
  }
}
