object Main extends App {
	
	def fun(a: Long, b: Long): Long = {
		if(a> b){
			return fun(b, a)
		}
		if(a == b){
			return a
		}else{
			if(b%a == 0){
				return 2*a + fun(a, b-a)
			}else{
				return 2* math.floor(b/a).toLong * a + fun(a, b%a)
			}
		}
	}

	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(x1==y1){
			ans += x1
			flag = false
		}else{
			if(y1%x1 == 0){
				ans += 2*x1
				y1 = y1 - x1
			}else{
				ans += 2*math.floor(y1/x1).toLong * x1
				y1 = y1%x1
			}
		}
	}

	//println(fun(x,y) + n)
	println(ans)

}