#include <iostream> 
#include <string> 
#include <vector> 
#include <cmath> 
#include <algorithm> 
#include <cstdlib> 
#include <ctime> 
#include <cstdio> 
#include <functional> 
#include <set> 
#include <sstream> 
#include <cctype>
#include <queue>

using namespace std; 

typedef pair<int,int> pii;
const int INF=(1<<30);

int popcount(int x){
	int res = 0;

	while(x){
		x &= x-1;
		res++;
	}

	return res;
}


int main(){


	int N;
	cin>>N;

	vector<int> d(N+1,INF);
	queue< int > q;
	q.push(1);
	d[1]=1;

	while(!q.empty()){
		int n=q.front(); q.pop();
		int m=popcount(n);
		
		if(n==N){cout<<d[n]<<endl; return 0;}

		if(n+m<=N && d[n+m]==INF){
			q.push(n+m);
			d[n+m]=d[n]+1;
		}

		if(n-m>0 && d[n-m]==INF){
			q.push(n-m);
			d[n-m]=d[n]+1;
		}

	}

	cout<<-1<<endl;

	return 0;
}