import java.util.ArrayDeque fun main() { val builder = StringBuilder() val n = readInputLine().toInt() val queue = ArrayDeque() val dp = IntArray(n + 1) { Int.MAX_VALUE } dp[1] = 1 queue.add(1) while (queue.isNotEmpty()) { val current = queue.poll() val bitCnt = current.toString(2).count { it == '1' } if (current - bitCnt > 0 && dp[current - bitCnt] > dp[current] + 1) { dp[current - bitCnt] = dp[current] + 1 queue.add(current - bitCnt) } if (current + bitCnt <= n && dp[current + bitCnt] > dp[current] + 1) { dp[current + bitCnt] = dp[current] + 1 queue.add(current + bitCnt) } } builder.appendln(if (dp[n] == Int.MAX_VALUE) -1 else dp[n]) print(builder.toString()) } fun readInputLine(): String { return readLine()!! }