#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007LL;
const int INF = 1000000007;
long long N, X;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> X;
  long long ans = N;
  bool flag = true;
  long long a = max(X, N - X);
  long long b = min(X, N - X);
  while (flag) {
    long long sy = a / b;
    long long am = a % b;
    ans += (sy + 1) * b;
    if (am == 0) flag = false;
    a = b;
    b = am;
  }
  cout << ans << endl;
  return 0;
}
