#include <bits/stdc++.h>
using namespace std;
const int INF = (long long int)(1LL << 31) - 1;
const long long int INFl = (long long int)9223372036854775807;
const int MAX = 10000;
const long long int MOD = (long long int)1e9 + 7;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
long long int n, x;
int f(long long int a, long long int b) {
  if (a == 0) {
    return -b;
  } else
    return (b / a) * a * 2 + f(b % a, a);
}
int main() {
  cin >> n >> x;
  long long int a = n - x, b = x;
  cout << n + f(a, b) << endl;
  return 0;
}
