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 Go (1.6) (judge verdict: Runtime Error)

package main

/*
     o
    o o
   o o o
  o o o o
 o o o o o
o o o o o o
*/

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
    //  "time"
)

func makePyramid(n int) [][]int {
    //p := make([x+1][x*2+1]int)
    p := make([][]int, n+1)
    for i := 0; i < n+1; i++ {
        p[i] = make([]int, n*2+1)
    }

    for y := 0; y < n+1; y++ {
        p[y][n-y] = '/'
        p[y][n+y] = '\\'
    }
    for x := 0; x < n+1; x++ {
        p[n][n-x] = '-'
        p[n][n+x] = '-'
    }

    return p
}

func dumpPyramid(p [][]int) {
    for y := 0; y < len(p); y++ {
        for x := 0; x < len(p[y]); x++ {
            c := p[y][x]
            if c == 0 {
                fmt.Print(" ")
            } else {
                fmt.Printf("%c", p[y][x])
            }
        }
        fmt.Println("")
    }
}

func readIntArray(bio *bufio.Reader) []int {
    s, _ := bio.ReadString('\n')
    valsStr := strings.Split(strings.Trim(s, "\n"), " ")
    vals := []int{}
    for _, s := range valsStr {
        val, _ := strconv.Atoi(s)
        vals = append(vals, val)
    }
    return vals
}

type Pos struct {
    x int
    y int
}

type Dir struct {
    dx int
    dy int
}

func proceed(pos Pos, dir Dir, p [][]int) (Pos, Dir) {
    nextPos := Pos{pos.x + dir.dx, pos.y + dir.dy}
    if p[nextPos.y][nextPos.x] == 0 {
        switch {
        case dir.dx == -2 || dir.dx == 2:
            p[nextPos.y][nextPos.x] = '-'
        case dir.dx == dir.dy:
            p[nextPos.y][nextPos.x] = '\\'
        default:
            p[nextPos.y][nextPos.x] = '/'
        }
    } else {
        switch p[nextPos.y][nextPos.x] {
        case '-':
            dir.dy = -dir.dy
        case '/':
            if dir.dx == -2 {
                dir.dx = 1
                dir.dy = 1
            } else if dir.dx == 2 {
                dir.dx = -1
                dir.dy = -1
            } else if dir.dx == -1 {
                dir.dx = 2
               dir.dy = 0
            } else {
                dir.dx = -2
                dir.dy = 0
            }
        case '\\':
            if dir.dx == -2 {
                dir.dx = 1
                dir.dy = -1
            } else if dir.dx == 2 {
                dir.dx = -1
                dir.dy = 1
            } else if dir.dx == -1 {
                dir.dx = 2
                dir.dy = 0
            } else {
                dir.dx = -2
                dir.dy = 0
            }
        default:
            panic("unknown")
        }
    }
    return nextPos, dir

}

func solve(n int, x int) int {
    p := makePyramid(n)
    //dumpPyramid(p)

    startPos := Pos{n - x, x}
    curPos := startPos
    curDir := Dir{2, 0}
    length := 0
    for {
        //fmt.Printf("pos=%v, dir=%v\n", curPos, curDir)
        nextPos, nextDir := proceed(curPos, curDir, p)
        length++
        if startPos == nextPos {
            break
        }
        curPos = nextPos
        curDir = nextDir

        //dumpPyramid(p)

        //time.Sleep(1 * time.Second)
    }

    return length
}

func main() {

    bio := bufio.NewReader(os.Stdin)
    vals := readIntArray(bio)
    fmt.Printf("%v\n", solve(vals[0], vals[1]))
}