#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
using lint=int64_t;

int bit[10010];
int bitcnt(int n)
{
	if(bit[n]!=0)
		return bit[n];
	
	int ret=0;
	while(1)
	{
		if(n==0)break;
		ret+=n%2;
		n/=2;
	}
	bit[n]=ret;
	return ret;
}

int main()
{
	int N;
	
	cin >> N;

	int ans=-1;
	bool visited[10010]={};
	queue<pair<int,int>> que;
	que.push(make_pair(1,1));
	while(!que.empty())
	{
		int d=que.front().first;
		int cur=que.front().second;
		que.pop();
		
		if(cur==N)
		{
			ans=d;
			break;
		}
		int cnt=bitcnt(cur);
		
		if(cur+cnt<=N && !visited[cur+cnt])
		{
			visited[cur+cnt]=true;
			que.push(make_pair(d+1,cur+cnt));
		}
		if(1<=cur-cnt && !visited[cur-cnt])
		{
			visited[cur-cnt]=true;
			que.push(make_pair(d+1,cur-cnt));
		}
	}

	cout << ans << endl;
	return 0;
}