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