#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1e9 + 7;
const int INF = 1 << 30;
int main() {
  long long N, X;
  cin >> N >> X;
  long long ans = N;
  long long a = N - X;
  long long b = X;
  while (a % b) {
    ans += (a / b + 1) * b;
    long long A = a, B = b;
    a = B;
    b = A % B;
  }
  ans += (a / b + 1) * b;
  cout << ans << endl;
}
