#include <bits/stdc++.h>
using namespace std;
void CINT() {}
template <class Head, class... Tail>
void CINT(Head&& head, Tail&&... tail) {
  cin >> head;
  CINT(move(tail)...);
}
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 1;
long long solve(long long n, long long x) {
  long long tmp = n - x;
  if (tmp == 0) return 0;
  long long res = 0;
  if (x == tmp) {
    res = 3 * x;
  } else if (x > tmp) {
    res += tmp * 2 * (x / tmp);
    if (x % tmp == 0) return res;
    res += tmp + solve(tmp, x % tmp);
  } else {
    res += x * 2 * (tmp / x);
    if (tmp % x == 0) return res;
    res += x * 2 * (tmp / x) + solve(tmp % x, x);
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, X;
  CINT(N, X);
  long long ans = solve(N, X);
  cout << ans << endl;
  return 0;
}
