#include <bits/stdc++.h>
using ull = unsigned long long int;
using namespace std;
int main() {
  ull N, X, next_N, next_X;
  cin >> N >> X;
  ull ans = 0;
  for (;;) {
    ans += N;
    ans += ((N - X) / X) * X * 2;
    next_N = X;
    next_X = N % X;
    N = next_N;
    X = next_X;
    if (X == 0) {
      ans -= next_N;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
