using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var str = Console.ReadLine().Split();
            var n = Int32.Parse(str[0]);
            var x = Int32.Parse(str[1]);
            Console.WriteLine(Solver.Solve(n, x));
        }
    }

    public class Solver
    {
        public static int Solve(int n, int x)
        {
            var ret = x + (n - x);
            ret += dfs(x, n - x);
            return ret;
        }

        public static int dfs(int a, int b)
        {
            if (a == 0 || b == 0) return 0;
            if (b % a == 0)
            {
                return a * 2 * (b / a - 1) + a;
            }

            var ret = (2 * a) * (b / a);
            if (a % (b % a) == 0) return ret + ((b % a) * 2) * (a / (b % a)) - b % a;

            ret += ((b % a) * 2) * (a / (b % a));
            return ret + dfs(a % (b % a), b % a);
        }
    }

}
