using System;
//using System.Collections.Generic;
//using System.Linq;
class Program
{
    static void Main()
    {
        string[] s = Console.ReadLine().Split();
        long n, x;
        n = long.Parse(s[0]);
        x = long.Parse(s[1]);
        C.WriteLine(f(n, x));
    }
    static long f(long n, long x)
    {
        if (x == 0)
            return 0;
        if (x <= (double)n / 2)
            return f(n - x, n - 2 * x) + 3 * x;
        if (x > (double)n / 2)
            return f(x, 2 * x - n) + 3 * n - 3 * x;
        return 0;
    }
}
//以下標準出力用のクラス
static class C
{
    [System.Runtime.InteropServices.DllImport("msvcrt.dll")]
    private static extern void printf(string s);
    public static void Write(string s) { printf(s); }
    public static void Write(char c) { printf(c.ToString()); }
    public static void Write(long n) { printf(n.ToString()); }
    public static void Write(int n) { printf(n.ToString()); }
    public static void Write(double d) { printf(d.ToString()); }
    public static void WriteLine() { printf("\n"); }
    public static void WriteLine(string s) { printf(s + "\n"); }
    public static void WriteLine(char c) { WriteLine(c.ToString()); }
    public static void WriteLine(long n) { WriteLine(n.ToString()); }
    public static void WriteLine(int n) { WriteLine(n.ToString()); }
    public static void WriteLine(double d) { WriteLine(d.ToString()); }
    public static void WriteLines(string[] s) { foreach (var i in s) WriteLine(i); }
    public static void WriteLines(char[] c) { foreach (var i in c) WriteLine(i); }
    public static void WriteLines(long[] c) { foreach (var i in c) WriteLine(i); }
    public static void WriteLines(int[] c) { foreach (var i in c) WriteLine(i); }
    public static void WriteLines(double[] d) { foreach (var i in d) WriteLine(i); }
}