Problem:
Score : 500 points

Problem Statement

Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light.

Three mirrors of length N are set so that they form an equilateral triangle.
Let the vertices of the triangle be a, b and c.

Inside the triangle, the rifle is placed at the point p on segment ab such that ap = X.
(The size of the rifle is negligible.)
Now, the rifle is about to fire a ray of Mysterious Light in the direction of bc.

The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light.
There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror!
When the ray comes back to the rifle, the ray will be absorbed.

The following image shows the ray's trajectory where N = 5 and X = 2.

It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of N and X.
Find the total length of the ray's trajectory.

Constraints

2≦N≦10^{12}

1≦X≦N-1

N and X are integers.

Partial Points

300 points will be awarded for passing the test set satisfying N≦1000.

Another 200 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N X

Output

Print the total length of the ray's trajectory.

Sample Input 1

5 2

Sample Output 1

12

Refer to the image in the Problem Statement section.
The total length of the trajectory is 2+3+2+2+1+1+1 = 12.

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

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

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        long[] inpt = Reader.ReadLine().Split(' ').Select(a=>long.Parse(a)).ToArray();
        long hen = inpt[0];
        this.Root = new TreeNode(0);
        this.DepthList.Add(0, new Dictionary<long, TreeNode>());
        this.DepthList[0].Add(0, this.Root);

        long idx = 1;
        for(long i=0; i<hen; i++) {
            this.DepthList.Add(this.DepthList.Count, new Dictionary<long, TreeNode>());
            for(long j=0; j<this.DepthList[i].Count; j++) {
                for(int k=0; k<2; k++) {
                    TreeNode tree = this.DepthList[i][j].AddChild(new TreeNode(idx));
                    if(tree == null) {
                        continue;
                    }
                    idx++;
                    this.DepthList[i + 1].Add(DepthList[i+1].Count, tree);
                }
            }
        }

        long ans = 0;

        Dir d = Dir.Right;

        TreeNode current = this.DepthList[inpt[1]][0];
        for(long i=0; i<this.DepthList.Sum(a=>a.Value.Count); i++) {
            Dir next = this.Turn(current, d);
            if(next == Dir.None) {
                break;
            }
            ans++;
            current.Visited = true;
            if(next == Dir.DownLeft) {
                current.HasMoveDownLeft = true;
                current = current.LeftChild;
            } else if(next == Dir.DownRight) {
                current.HasMoveDownRight = true;
                current = current.RightChild;
            } else if(next == Dir.UpLeft) {
                current.HasMoveUpLeft = true;
                current = current.LeftParent;
            } else if(next == Dir.UpRight) {
                current.HasMoveUpRight = true;
                current = current.RightParent;
            } else if(next == Dir.Left) {
                current.HasMoveLeft = true;
                current = current.PrevSibling;
            } else if(next == Dir.Right) {
                current.HasMoveRight = true;
                current = current.NextSibling;
            }
            d = next;
        }
        Console.WriteLine(ans);
    }

    private Dir Turn(TreeNode trr, Dir now) {
        if(now == Dir.Left) {
            if(trr.HasMoveUpLeft && trr.HasMoveDownRight) {
                if(!trr.HasMoveUpRight) {
                    return Dir.UpRight;
                }
            } else if(trr.HasMoveUpRight && trr.HasMoveDownLeft) {
                if(!trr.HasMoveDownRight) {
                    return Dir.DownRight;
                }
            } else if(trr.PrevSibling == null) {
                if(!trr.HasMoveDownRight) {
                    return Dir.DownRight;
                }
            } else if(!trr.HasMoveLeft)
            {
                return now;
            }
        } else if(now == Dir.Right) {
            if(trr.HasMoveUpLeft && trr.HasMoveDownRight) {
                if(!trr.HasMoveDownLeft) {
                    return Dir.DownLeft;
                }
            } else if(trr.HasMoveUpRight && trr.HasMoveDownLeft) {
                if(!trr.HasMoveUpLeft) {
                    return Dir.UpLeft;
                }
            } else if(trr.NextSibling == null) {
                if(!trr.HasMoveDownLeft) {
                    return Dir.DownLeft;
                }
            } else if(!trr.HasMoveRight) {
                return now;
            }
        } else if(now == Dir.DownLeft) {
            if(trr.HasMoveUpLeft && trr.HasMoveDownRight) {
                if(!trr.HasMoveRight) {
                    return Dir.Right;
                }
            } else if(trr.HasMoveLeft && trr.HasMoveRight) {
                if(!trr.HasMoveUpLeft) {
                    return Dir.UpLeft;
                }
            } else if(trr.LeftChild == null) {
                if(!trr.HasMoveUpLeft) {
                    return Dir.UpLeft;
                }
            } else if(!trr.HasMoveDownLeft) {
                return now;
            }
        } else if(now == Dir.DownRight) {
            if(trr.HasMoveUpRight && trr.HasMoveDownLeft) {
                if(!trr.HasMoveLeft) {
                    return Dir.Left;
                }
            } else if(trr.HasMoveLeft && trr.HasMoveRight) {
                if(!trr.HasMoveUpRight) {
                    return Dir.UpRight;
                }
            } else if(trr.RightChild == null) {
                if(!trr.HasMoveUpRight) {
                    return Dir.UpRight;
                }
            } else if(!trr.HasMoveDownRight)
            {
                return now;
            }
        } else if(now == Dir.UpLeft) {
            if(trr.HasMoveDownLeft && trr.HasMoveUpRight) {
                if(!trr.HasMoveRight) {
                    return Dir.Right;
                }
            } else if(trr.HasMoveRight && trr.HasMoveLeft) {
                if(!trr.HasMoveDownLeft) {
                    return Dir.DownLeft;
                }
            } else if(trr.LeftParent == null) {
                if(!trr.HasMoveRight) {
                    return Dir.Right;
                }
            } else if(!trr.HasMoveUpLeft) {
                return now;
            }
        } else if(now == Dir.UpRight) {
            if(trr.HasMoveDownRight && trr.HasMoveUpLeft) {
                if(!trr.HasMoveLeft) {
                    return Dir.Left;
                }
            } else if(trr.HasMoveLeft && trr.HasMoveRight) {
                if(!trr.HasMoveDownRight) {
                    return Dir.DownRight;
                }
            } else if(trr.RightParent == null) {
                if(!trr.HasMoveLeft) {
                    return Dir.Left;
                }
            }  else if(!trr.HasMoveUpRight)
            {
                 return now;
            }
        }
        return Dir.None;
    }

    private enum Dir {
        UpLeft,
        UpRight,
        DownLeft,
        DownRight,
        Left,
        Right,
        None
    }

    private TreeNode Root;

    private Dictionary<long, Dictionary<long, TreeNode>> DepthList = new Dictionary<long, Dictionary<long, TreeNode>>();

    public class TreeNode {
        public bool Visited = false;
        public TreeNode LeftParent;
        public TreeNode RightParent;
        public TreeNode NextSibling;
        public TreeNode PrevSibling;

        public TreeNode LeftChild;
        public TreeNode RightChild;

        private bool mMoveRight = false;
        public bool HasMoveRight {
            get{return this.mMoveRight;}
            set{
                this.mMoveRight = value;
                if(this.NextSibling != null && this.NextSibling.HasMoveLeft != value) {
                    this.NextSibling.HasMoveLeft = value;
                }
            }
        }

        private bool mMoveLeft = false;

        public bool HasMoveLeft {
            get{return this.mMoveLeft;}
            set {
                this.mMoveLeft = value;
                if(this.PrevSibling != null && this.PrevSibling.HasMoveRight != value) {
                    this.PrevSibling.HasMoveRight = value;
                }
            }
        }

        private bool mMoveDownRight = false;

        public bool HasMoveDownRight {
            get {
                return this.mMoveDownRight;
            }
            set {
                this.mMoveDownRight = value;
                if(this.RightChild != null && this.RightChild.HasMoveUpLeft != value) {
                    this.RightChild.HasMoveUpLeft = value;
                }
            }
        }
        private bool mMoveDownLeft = false;

        public bool HasMoveDownLeft {
            get {
                return this.mMoveDownLeft;
            }
            set {
                this.mMoveDownLeft = value;
                if(this.LeftChild != null && this.LeftChild.HasMoveUpRight != value) {
                    this.LeftChild.HasMoveUpRight = value;
                } 
            }
        }
        private bool mMoveUpRight = false;

        public bool HasMoveUpRight {
            get { return this.mMoveUpRight;}
            set {
                this.mMoveUpRight = value;
                if(this.RightParent != null && this.RightParent.HasMoveDownLeft != value) {
                    this.RightParent.HasMoveDownLeft = value;
                }
            }
        }
        private bool mMoveUpLeft = false;

        public bool HasMoveUpLeft {
            get {return this.mMoveUpLeft;}
            set {
                this.mMoveUpLeft = value;
                if(this.LeftParent != null && this.LeftParent.HasMoveDownRight != value) {
                    this.LeftParent.HasMoveDownRight = value;
                }
            }
        }
        public long Id;
        public TreeNode(long id) {
            this.Id = id;
        }

        public int Depth;
        public TreeNode AddChild(TreeNode addChild) {
            if(this.LeftChild == null) {
                this.LeftChild = addChild;
                addChild.RightParent = this;
                addChild.Depth = this.Depth + 1;
                return addChild;
            } else if(this.RightChild == null) {
                this.RightChild = addChild;
                addChild.LeftParent = this;
                this.LeftChild.NextSibling = addChild;
                addChild.PrevSibling = this.LeftChild;
                if(this.NextSibling != null) {
                    this.NextSibling.LeftChild = addChild;
                    addChild.RightParent = this.NextSibling;
                }
                addChild.Depth = this.Depth + 1;
                return addChild;
            }
            return null;
        }
    }

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


5 2

 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }

    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}