#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <class T1, class T2>
void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
const long long inf = 1LL << 55;
const long long mod = 1e9 + 7;
long long f(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == b) return a;
  return 2 * a + f(a, b - a);
}
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);
  long long N, X;
  cin >> N >> X;
  assert(N <= 1000);
  cout << f(N - X, X) + N << endl;
  return 0;
}
