package main

import "fmt"

func main() {
	n, x := 0, 0
	fmt.Scan(&n)
	fmt.Scan(&x)
	fmt.Println(3 * (n - gcd(n, x)))
}

func min(n, x int) int {
	if n < x {
		return n
	}
	return x
}
func gcd(n, x int) int {
	for i := min(n, x); i > 0; i-- {
		if n&i == 0 && x&i == 0 {
			return i
		}
	}
	return 1
}
