package yukicoder import kotlin.math.absoluteValue fun main() { val n: Int = readLine()!!.toInt() val sugoroku: List = (1..n).map { getBit(it) } var cache: MutableSet = mutableSetOf() fun canReachFromZero(target: Int, deep: Int): Int { return when { target == 0 -> deep cache.contains(target) -> -1 else -> { cache.add(target) val fromList: List = (0 until sugoroku.size).filter { (target - it).absoluteValue == sugoroku[it] } fromList.map { canReachFromZero(it, deep + 1) }.find { it != -1 } ?: run { -1 } } } } println(canReachFromZero(n - 1, 1)) } fun getBit(number: Int): Int = when (number) { 0 -> 0 1 -> 1 else -> getBit(number / 2) + number % 2 }