using System;
using System.Collections.Generic;
using System.Linq;

namespace No00003_BitSugoroku
{
    internal class Program
    {
        static void Main(string[] args) {
            int n = int.Parse(Console.ReadLine());

            int[] map = new int[n + 1];
            map[1] = 1;
            Queue<int> q = new Queue<int>();
            q.Enqueue(1);

            while (0 < q.Count) {
                int x = q.Dequeue();
                int diff = Convert.ToString(x, 2).Count(x => x == '1');

                if (x + diff <= n) {
                    if (map[x + diff] == 0 || map[x] < map[x + diff]) {
                        q.Enqueue(x + diff);
                        map[x + diff] = map[x] + 1;
                    }
                }
                if (0 < x - diff) {
                    if (map[x - diff] == 0 || map[x] < map[x - diff]) {
                        q.Enqueue(x - diff);
                        map[x - diff] = map[x] + 1;
                    }
                }
            }
            Console.WriteLine(map[n] == 0 ? -1: map[n]);
        }
    }
}