#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int mod = 1e9 + 7, INF = 1 << 30;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll LINF = 1LL << 60, lmod = 1e9 + 7;
const int MAX_N = 100005;
int main() {
  ll N, X, ans;
  cin >> N >> X;
  ans = N + X;
  ll a = max(X, N - 2 * X);
  ll b = min(X, N - 2 * X);
  while (b != 0) {
    if (a % b == 0)
      ans += (2 * (a / b) + 1) * b;
    else
      ans += 2 * (a / b) * b;
    a = max(a % b, b);
    b = min(a % b, b);
  }
  cout << ans << endl;
  return 0;
}
