#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define clear(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
LL n, x;
LL f(LL a, LL b) {
	if(a > b) swap(a, b);
	if(a == 1) return b * 2 - 1;
	return 1ll * f(b - a, a) + 2 * a;
}
LL ans;
int main(){
	scanf("%lld %lld", &n, &x);
	ans = n + f(x, n - x)
	printf("%lld\n", ans);
	return 0;
}
