#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X;
  cin >> N >> X;
  if (N % X == 0) {
    cout << (N - X) * 2 * X + N - X << endl;
  } else {
    long long T = N / X;
    long long h = N - X;
    long long ans = 0;
    ans += N;
    long long n = X;
    while (1) {
      if (h % n == 0) {
        ans += h / n;
        ans++;
        break;
      }
      long long a = h / n;
      long long b = h % n;
      ans += n * a * 2;
      h = n;
      n--;
    }
    cout << ans << endl;
  }
}
