#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long mod = 1e9 + 7;
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  long long N, X;
  cin >> N >> X;
  X = min(X, N - X);
  long long ans = ((N - X) / X) * 3 * X;
  long long A = (N - X) % X;
  if (A != 0) {
    ans += (X / A) * 3 * A;
  }
  cout << ans << endl;
}
