#include <bits/stdc++.h>
using namespace std;

#define DEBUG_ON 1  // 0 = off, 1 = on

namespace {
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define LEN(x) (int)((x).size())
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define OP minmax

#define FOR(i, begin, end)  \
  for (__typeof(end) i = (begin) - ((begin) > (end));  \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#ifndef ONLINE_JUDGE
#include "debug.h"
#endif  // ONLINE_JUDGE
  
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<ll> vll;
template<typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

ll expmod(ll a,ll b,ll mod) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
}

const int MAX_N = 200000;
const ll MODD = 1000000009LL;
const ld EPS = 1e-9;



ll a, b, c;
ll ba, bb, bc;

ll solve(ll N, ll X) {
	if (N % 2 == 0 && X == N / 2) return 3 * X;
	if (2*X > N) X = N - X;
	
	ll ans = 2 * X + N;
	
	ll rem = (N - 2*X);
	ll x = X;
	while (rem != 0) {
		DEBUG(ans, rem, x);
		if (rem % x == 0) {
			ans += 2 * rem - x;
			return ans;
		}
		ans += (rem / x) * 2*x;
		ll temp = x;
		x = rem % x;
		rem = temp;
	}
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	ll N, X;
	cin >> N >> X;
	
	cout << solve(N, X) << endl;
}
