import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int [] c = new int [N+1]; for(int i=2; i<=N; i++){ c[i]=10000; } for(int i=1; i<=N; i++){ int t=Integer.bitCount(i); go(c,i,N); if(i-t>=1&&c[i-t]>c[i]+1){ c[i-t]=c[i]+1; go(c,i-t,N); } } int ans=-1; if(c[N]!=10000){ ans=c[N]+1; } System.out.println(ans); } static int[] go(int []a, int i, int N){ int t=Integer.bitCount(i); while(i+t<=N){ a[i+t]=Math.min(a[i+t],a[i]+1); i=i+t; t=Integer.bitCount(i); } return a; } }