// ------------------------------------
// Date:2018/ 3/28
// Problem:Mysterious Light / b.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RFOR(i,a,b) for(int i=(int)b-1;i>=(int)a;--i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define debug(x) cerr << #x << ":" << x << endl;
#define OK(ok) cout << (ok ? "Yes" : "No") << endl;
typedef long long ll;

void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail) {
  cin >> head; CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)

const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e6 + 1;
const int MAX_M = 1e3 + 1;

ll memo[MAX_N][MAX_M];

ll solve(ll n, ll x)
{
  // debug(n);
  // debug(x);
  ll tmp = n - x;
  if (n <= MAX_N && x <= MAX_M && memo[n][x] > 0) return memo[n][x]; 
  if (tmp == 0) return 0;
  
  ll 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);
  }

  if (n > MAX_N or x > MAX_M) return res;
  else return memo[n][x] = res;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  LCIN(N, X);
  
  ll ans = solve(N, X);

  cout << ans << endl;
  
  return 0;
}

