#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void priv(vector<T> a) {
  for (int i = 0; i < a.size(); i++) {
    cerr << a[i] << ((i == a.size() - 1) ? "\n" : " ");
  }
}
long long gcd(long long a, long long b) {
  int c = max(a, b);
  int d = min(a, b);
  return c == 0 || d == 0 ? c : gcd(c % d, d);
}
long long lcm(long long a, long long b) {
  return a == 0 || b == 0 ? 0 : a * b / gcd(a, b);
}
long long fact(long long a) {
  long long b = 1;
  for (int i = 1; i <= a; i++) b *= i;
  return b;
}
long long power(long long x, long long n) {
  long long a = 1;
  for (int i = 0; i < n; i++) a *= x;
  return a;
}
long long f(long long m, long long n) {
  if (m == 0 || n == 0) return 0;
  long long a = max(m, n);
  long long b = min(m, n);
  return m + (2 * (a / b) - 1) * b + f(b, a % b);
}
int main() {
  int n, x;
  cin >> n >> x;
  cout << x + f(n - x, x) << endl;
}
