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]);
        long g = GCD(n, x);
        C.WriteLine(g * f(n / g, x / g));
    }
    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;
    }
    public static long GCD(long n, long m)
    {
        if (Math.Min(n, m) < 0)
            return -1;
        if (Math.Min(n, m) == 0)
            return Math.Max(n, m);
        return GCD(Math.Max(n, m) - Math.Min(n, m), Math.Min(n, m));
    }

}
//以下標準出力用のクラス
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); }
}