fun main(arg: Array<String>) {
    val (n, x) = listOfLong()
//val ans = 3 * (n - gcd(n, x))
    val ans = f(x, n - x) + n
    println(ans)
}
fun listOfLong() = readLine()!!.split(" ").map { it.toLong() }
tailrec fun gcd(a: Long, b: Long): Long = if (a % b == 0L) b else gcd(b, a % b)
tailrec fun f(a : Long, b : Long) : Long {
    if (b == 0L) return -a
    if (a < b) return f(b, a)
    else {
        val m = a / b
        return m * 2 * b + f(b, a % b)
    }
}
