import java.io.*; import java.util.Arrays; import java.util.Deque; import java.util.ArrayDeque; class Main{ final static int INF=Integer.MAX_VALUE/2; public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String line=""; int[] map=new int[10001]; Arrays.fill(map,INF); Deque que=new ArrayDeque(); map[1]=1; que.offerLast(1); while(que.peekFirst()!=null){ int n=que.pollFirst(); int noo=Integer.bitCount(n); if(n-noo>=1&&map[n-noo]>map[n]+1){ map[n-noo]=map[n]+1; que.offerLast(n-noo); } if(n+noo<=10000&&map[n+noo]>map[n]+1){ map[n+noo]=map[n]+1; que.offerLast(n+noo); } } while((line=br.readLine())!=null){ int t=Integer.parseInt(line); System.out.println(map[t]==INF?-1:map[t]); } } }