#include <bits/stdc++.h>
using namespace std;
long long n, x, ans = 0, now, sum;
int main() {
  cin >> n >> x;
  ans += n;
  now = x, sum = n - x;
  while (1) {
    if (sum % now == 0) {
      ans += (sum / now + 1) * now;
      cout << ans;
      return 0;
    } else {
      ans = ans + (sum / now + 1) * now;
      swap(now, sum);
      now = now % sum;
      if (now < 0) now += sum;
    }
  }
}
