#include <bits/stdc++.h>
using namespace std;
void Main() {
  long long N, X;
  cin >> N >> X;
  long long res = N;
  long long a = N - X;
  long long b = X;
  while (b != 0) {
    long long div = a / b;
    long long mod = a % b;
    res += (div + 1) * b;
    a = b;
    b = mod;
  }
  cout << res << endl;
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  Main();
}
