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