#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <sstream>
#include <cmath>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()

typedef pair<int, int> Pii;
typedef vector<int> V;
typedef long long ll;
const int inf = 1e9;
const int mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	if(a%b==0) return b;
	return gcd(b,a%b);
}

int main()
{
	ll N, X;
	cin >> N >> X;

	cout << 3*(N - gcd(N,X)) << endl;

	retur