#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 dfs(int a, int b) {
  if (a < b) swap(a, b);
  if (a % b == 0) return a * 2 - b;
  return 2 * (a / b) * b + dfs(a % b, b);
}
int main() {
  int N, X;
  cin >> N >> X;
  cout << N + dfs(N - X, X) << endl;
  return 0;
}
