using System; namespace BitSugoroku { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int R = MainProcess(N); Console.WriteLine(R); } private static int MainProcess(int N) { int[] Squares = new int[N + 1]; Squares[1] = 1; int bits = 0; bool newdiscovery = true; while (newdiscovery) { newdiscovery = false; for (int x = 1; x <= N; x++) { if (Squares[x] > 0) { bits = BitCounter(x); if (x + bits <= N && (Squares[x + bits] == 0 || Squares[x + bits] > Squares[x] + 1)) { Squares[x + bits] = Squares[x] + 1; newdiscovery = true; } if (x - bits >= 0 && (Squares[x - bits] == 0 || Squares[x - bits] > Squares[x] + 1)) { Squares[x - bits] = Squares[x] + 1; newdiscovery = true; } } } } return (Squares[N] == 0) ? -1 : Squares[N]; } private static int BitCounter(int x) { int b = 0; while (x > 0) { b += x % 2; x /= 2; } return b; } } }