object Main extends App {

	var Array(n, x) = scala.io.StdIn.readLine.split(" ").map(_.toLong)
	
	var x1 = x
	var y1 = n - x

	var flag = true
	var ans = n
	
	while(flag){
		if(x1 > y1){
			val tp = x1
			x1 = y1
			y1 = tp
		}

		if(y1%x1 == 0){
			ans += x1 * ( 2* (y1 / x1) - 1)
			flag = false
		}else{
			ans += x1 * 2 * (y1 / x1)
			y1 = y1 % x1
		}
	}

	println(ans)

}
