#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 2000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
int main() {
ll x, y, ans = 0;
scanf("%lld %lld", &x, &y);
while (x && y) {
swap(x, y);
ans += 4 * y * (x / y);
x %= y;
}
printf("%lld", ans);
return 0;
}