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="";
while((line=br.readLine())!=null){
int t=Integer.parseInt(line);
int[] map=new int[t+1];
Arrays.fill(map,INF);
Deque<Integer> que=new ArrayDeque<Integer>();
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<=t&&map[n+noo]>map[n]+1){
map[n+noo]=map[n]+1;
que.offerLast(n+noo);
}
}
System.out.println(map[t]==INF?-1:map[t]);
}
}
}