#include <bits/stdc++.h>
using namespace std;
template <typename T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <typename T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);
int main() {
  int N, X;
  cin >> N >> X;
  int a = N - X;
  int b = X;
  if (a < b) swap(a, b);
  int res = N;
  while (a % b) {
    res += 2 * (a / b) * b;
    a %= b;
    if (a < b) swap(a, b);
  }
  res += a * 2 - b;
  cout << res << endl;
  return 0;
}
