#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios ::sync_with_stdio(0);
  ll n, x;
  cin >> n >> x;
  ll ans = 0;
  while (1) {
    ans += x + n;
    x = n - x;
    n = n - x;
    n %= x;
    if (n == 0) break;
    swap(n, x);
    x = n - x;
    ans -= x;
  }
  cout << ans << '\n';
}
