#include <bits/stdc++.h>
using namespace std;
int N, X;
int R(int n, int x) {
  int k = (n - x) / x;
  int r = (n - x) % x;
  int res = n + x * k + x * (k - 1);
  if (r == 0) return res;
  int kk = x / r;
  int rr = x % r;
  if (rr == 0) return res + x + r * kk + r * (kk - 1);
  return res + x + r * kk + r * (kk - 1) + R(rr + r, rr) - rr;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> X;
  cout << R(N, X) << '\n';
  return 0;
}
