#include <bits/stdc++.h>
using namespace std;
void read_file(bool outToFile = true) {}
long long f(int x, int n) {
  if (x == 0) return 0;
  assert(n > x);
  int q = n / (n - x);
  long long ret = 2 * q * (n - x) - 1;
  return ret + f(n - q * (n - x), n - x);
}
int main() {
  read_file();
  int n, x;
  while (cin >> n >> x) {
    long long ans = f(x, n) + x;
    cout << ans << '\n';
  }
}
