#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
int64 f(int64 n, int64 diff) {
  int64 res = 0;
  if (diff == 0) {
    res += 0;
  } else if (n % diff == 0) {
    res += diff * (n / diff) + diff;
  } else {
    res += n;
    res += min(n, diff);
    res += f(n, abs(n - diff));
  }
  cerr << n << " " << diff << "\n";
  cerr << res << "\n";
  return res;
}
signed main() {
  int64 n, x;
  cin >> n >> x;
  int64 ans = x + (n - x) + f(x, n - x);
  cout << ans << endl;
  return 0;
}
