
object Main extends App {
	val Array(n,x) = scala.io.StdIn.readLine.split(" ").map(_.toLong)

	def tra(a: Long, b: Long): Long = {
		if(a==b) a
		else if(a > b) tra(a-b,b) + 2*b + a
		else tra(a,b-a) + 2*a + b
	}	

	println(tra(x,n-x))
}