#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X;
  cin >> N >> X;
  X = min(X, N - X);
  long long ans;
  if (N % X == 0) {
    ans = ((N - X) / X) * (X * 3);
  } else {
    while (N % X != 0) {
      X--;
    }
    ans = ((N - X) / X) * (X * 3);
  }
  cout << ans << endl;
  return 0;
}
