#include <bits/stdc++.h>
using namespace std;
long long N, X;
long long Solve(long long a, long long b) {
  if (b == 0) return -1;
  return (a / b) * b * 2 + Solve(b, a % b);
}
signed main() {
  cin >> N >> X;
  printf("%lld\n", N + Solve(N - X, X));
  return 0;
}
