using System.Linq;
using System.Collections.Generic;
using System;

class A {
    static int N, X;
    static void Main() {
        string[] a = Console.ReadLine().Split(' ');
        N = int.Parse(a[0]);
        X = int.Parse(a[1]);
        if (N >= X * 2) {

            int cnt = 0;
            int q = N - X;
            int e = X;
            while (true) {
                if (q == 0) {
                    break;
                }
                cnt += 3 * e;
                int r = q;
                q = e;
                e = r - e;
            }
            Console.WriteLine(cnt);
        }
        else {
            int cnt = X;
            int q = N - X;
            int e = N;
            while (true) {
                cnt += ((e / q)+((e/q)-1)) * q;
                int r = q;
                q = e % q;
                e = r;
                if (q == 0) {
                    break;
                }
            }
            Console.WriteLine(cnt);
        }
    }
}