#include <bits/stdc++.h>
struct Edge {
  int from, to, cost;
  Edge(int from, int to, int cost) : from(from), to(to), cost(cost){};
};
inline int toInt(std::string s) {
  int v;
  std::istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline std::string toString(T x) {
  std::ostringstream sout;
  sout << x;
  return sout.str();
}
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
int main() {
  long long n;
  long long x;
  long long res = 0;
  cin >> n >> x;
  if (n - x < x) {
    x = n - x;
  }
  if (n % x == 0) {
    res = ((n / x) - 1) * (x * 3);
  } else {
    res = 3 * (n - 1);
  }
  cout << res << endl;
  return 0;
}
