#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <climits>
#include <cstdlib>
#include <vector>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define INF (INT_MAX/3)
#define PSB push_back
typedef long long LL;
//#define int long long

//booooooooooooost

#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;	//多倍長整数(mp::cpp_int)



signed main() {

	mp::cpp_int N, X;
	cin >> N >> X;
	
	
	mp::cpp_int ans = 0;
	mp::cpp_int a = N, b = X > N - X ? X : N - X;
	while (1) {
		mp::cpp_int tmp = b;
		if(a != N) ans += 3 * (a / b) * b;
		b = a % b;
		a = tmp;
		if (b == 0) break;
	}

	cout << ans << endl;

	return 0;
}