import scala.math._ import scala.collection.mutable.Queue object Main { def calc(n: Int): Int = { val used = new Array[Boolean](n+1) val q = new Queue[(Int, Int)] // [pos, step] q.enqueue((1, 1)) while (!q.isEmpty) { val (pos, step) = q.dequeue if (pos == n) return step if (!used(pos)) { used(pos) = true val m = Integer.bitCount(pos) if (pos - m > 0) q.enqueue((pos - m, step + 1)) if (pos + m <= n) q.enqueue((pos + m, step + 1)) } } return -1 } def main(args: Array[String]) { val sc = new java.util.Scanner(System.in) val n = sc.nextInt val ans = calc(n) println(ans) } }