Problem: Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = x × x, x3 = x2 × x, x4 = x3 × x, ... , x31 = x30 × x.

The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute x31 with eight multiplications:

x2 = x × x, x3 = x2 × x, x6 = x3 × x3, x7 = x6 × x, x14 = x7 × x7,
x15 = x14 × x, x30 = x15 × x15, x31 = x30 × x.

This is not the shortest sequence of multiplications to compute x31. There are many ways with only seven multiplications. The following is one of them:

x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x10 = x8 × x2,
x20 = x10 × x10, x30 = x20 × x10, x31 = x30 × x.

There however is no way to compute x31 with fewer multiplications. Thus this is one of the most eficient ways to compute x31 only by multiplications.

If division is also available, we can find a shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):

x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x16 = x8 × x8, x32 = x16 × x16,
x31 = x32 ÷ x.

This is one of the most eficient ways to compute x31 if a division is as fast as a multiplication.

Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence of operations should be x to a positive integer's power. In other words, x-3, for example, should never appear.



Input

The input is a sequence of one or more lines each containing a single integer n. n is positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output

Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.

Example

Input

1
31
70
91
473
512
811
953
0


Output

0
6
8
9
11
9
13
12

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;

const int inf=1001;


int min_res;
bool used[2048];

inline int hstar(int cmax,int t){
	int x=cmax,ret=0;
	while(x<t) x<<=1,ret++;
	return ret;
}

int ary[1010];

int dfs(int t,int x,int d,int len){
	if(used[t]) return min_res=d;
	const int cmax=*max_element(ary,ary+len);

	if(d+hstar(cmax,t)>=min_res) return inf;

	int ret=inf;
	
	bool made[2048];
	clr(made,false);
	
	rrep(i,len){
		const int s=ary[i];
		if(0<x+s && x+s<=2*t && used[x+s]==false && made[x+s]==false){
			made[x+s]=true;
			used[x+s]=true;
			ary[len]=x+s;
			chmin(ret,dfs(t,x+s,d+1,len+1));
			used[x+s]=false;
		}
	}

	rep(i,len){
		const int s=ary[i];
		if(0< x-s && x-s<=2*t && used[x-s]==false && made[x-s]==false){
			made[x-s]=true;
			used[x-s]=true;
			ary[len]=x-s;
			chmin(ret,dfs(t,x-s,d+1,len+1));
			used[x-s]=false;
		}
	}

	return ret;
}

int main(void){
	int n;
	while(cin >> n,n){
		clr(used,false);
		used[0]=used[1]=true;
		ary[0]=1;
		min_res=0;
		while(dfs(n,1,0,1)==inf) min_res++;
		cout  << min_res << endl;
	}
	return 0;
}