#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>
using namespace std;
const int INF = 0xffffff;
const double esp = 10e-8;
const double Pi = 4 * atan(1.0);
const int Maxn = 2000+10;
const long long mod = 2147483647;
const int dr[] = {1,0,-1,0,-1,1,-1,1};
const int dc[] = {0,1,0,-1,1,-1,-1,1};
typedef long long LL;
LL gac(LL a,LL b){
return b?gac(b,a%b):a;
}
int n,ans[Maxn],maxd;
bool vis[Maxn];
int dfs(int step,int s){
ans[step] = s;
if(step == maxd){
if(vis[n])
return 1;
return 0;
}
if(ans[step] > 2000 || s * 1 << (maxd-step) < n){
return 0;
}
for(int i = 0;i<= step;i++){
int t = s + ans[i];
if(!vis[t]){
vis[t] = 1;
if(dfs(step+1,t))
return 1;
vis[t] = 0;
}
t = abs(s-ans[i]);
if(t > 0 && !vis[t]){
vis[t] = 1;
if(dfs(step+1,t))
return 1;
vis[t] = 0;
}
}
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
while(~scanf("%d",&n) && n){
for(maxd = 0;;maxd++){
memset(vis,0,sizeof(vis));
vis[1] = 1;
if(dfs(0,1)){
printf("%d\n",maxd);
break;
}
}
}
return 0;
}