package net.ipipip0129.kotlin.yukicoder // 幅優先探索 fun main(args: Array) { val len = readLine()!!.toInt() val queue = mutableListOf(1) // ループ回避用 val isPass = Array(len, {false}) // カウント用 val countList = mutableListOf(1) isPass[0] = true while (queue.size != 0) { val np = queue[0] queue.removeAt(0) val count = countList[0] countList.removeAt(0) if (np == len) { println(count) return } val vp = Integer.toBinaryString(np).filter { s -> s == '1' }.length if (np + vp <= len) { if (!isPass[np + vp - 1]) { queue.add(np + vp) countList.add(count + 1) isPass[np + vp - 1] = true } } if (0 < np - vp) { if (!isPass[np - vp - 1]) { queue.add(np - vp) countList.add(count + 1) isPass[np - vp - 1] = true } } } println("-1") }