#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <random>

using namespace std;

static const double EPS = 1e-9;
template<class T> bool INRANGE(T x,T a,T b){return a<=x&&x<=b;}
template<class T> void amin(T &a,T v){if(a>v) a=v;}
template<class T> void amax(T &a,T v){if(a<v) a=v;}
int ROUND(double x) { return (int)(x+0.5); }
bool ISINT(double x) { return fabs(ROUND(x)-x)<=EPS; }
bool ISEQUAL(double x,double y) { return fabs(x-y)<=EPS*max(1.0,max(fabs(x),fabs(y))); }
double SQSUM(double x,double y) { return x*x+y*y; }
#define PI  (acos(-1))
#define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) 
#define NG (-1)
#define BIG ((int)1e9)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
typedef unsigned long long ull;
typedef long long ll;

#if 1 // B


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

	ll result = N;
	ll hi = max(X, N-X);
	ll lo = min(X, N - X);

	while (1)
	{
		if (hi%lo == 0)
		{
			ll numTri = hi / lo;
			result += lo * (2 * numTri - 1);
			break;
		}
		else
		{
			ll numTri = hi / lo;
			result += lo * (2 * numTri);
			hi = hi%lo;
			swap(hi, lo);
		}
	}

	cout << result << endl;

	return 0;
}


#elif 1 // C

typedef int Weight;
struct Edge {
	int src, dst;
	Weight weight;
	bool isEliminated;
	Edge(int src, int dst, Weight weight) :
		src(src), dst(dst), weight(weight), isEliminated(false) { }
};
bool operator < (const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
	e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

typedef pair<Weight, int> Result;



Result visit(int p, int v, const Graph &g) {
	Result r(0, v);
	for( auto& e : g[v])
	{
		if (e.isEliminated) continue;

		if (e.dst != p) {
			Result t = visit(v, e.dst, g);
			t.first += e.weight;
			if (r.first < t.first) r = t;
		}
	}
	return r;
}

Weight diameter(const Graph &g, int& farthest0, int& farthest1) {
	Result r = visit(-1, 0, g);
	Result t = visit(-1, r.second, g);

	farthest0 = r.second;
	farthest1 = t.second;

	return t.first; // (r.second, t.second) is farthest pair
}

int diameterAfterRemove(Graph& g, int eliminatedNode)
{
	for (Edges& es : g)
	{
		for (Edge& e : es)
		{
			if (e.src == eliminatedNode || e.dst == eliminatedNode)
			{
				e.isEliminated = true;
			}
		}
	}

	int farthest0, farthest1;
	int tyokkei = diameter(g, farthest0, farthest1);

	return tyokkei;
}

int main()
{
	int N, K;
	cin >> N >> K;

	Graph g;
	g.resize(N);


	set <int> removed;
	for (int i = 0; i < N - 1; ++i)
	{
		int src, dst;
		cin >> src >> dst;
		src--;
		dst--;
		g[src].push_back(Edge(src, dst, 1));
		g[dst].push_back(Edge(dst, src, 1));
	}

	int result = 0;
	while (1)
	{
		int farthest0;
		int farthest1;
		int tyokkei = diameter(g, farthest0, farthest1);

//		cout << tyokkei << " " << farthest0 << " " << farthest1 << endl;

		if (tyokkei <= K)
		{
			break;
		}
		else
		{
			Graph gdia0(g);
			Graph gdia1(g);

			int dia0 = diameterAfterRemove(gdia0, farthest0);
			int dia1 = diameterAfterRemove(gdia1, farthest1);
			if (dia0 < dia1)
			{
				g = gdia0;
			}
			else
			{
				g = gdia1;
			}

			result++;
		}

	}

	cout << result << endl;

	return 0;

}


#else // A



int main()
{
	int N;
	cin >> N;
	vector <int> L(2*N);
	for (int i = 0; i < 2 * N; ++i)
	{
		cin >> L[i];
	}

	sort(L.rbegin(), L.rend());
	int result = 0;
	for (int i = 0; i < N; ++i)
	{
		result += L[2 * i + 1];
	}

	cout << result << endl;

	return 0;
}

#endif

Measured memory_bytes: 256