import java.io.*; import java.util.*; /*yukicoder No.3 ビットすごろく*/ class No3{ public static void main(String[] args){ final int NUMBER = 0; final int COST =1; Scanner scan = new Scanner(System.in); //ゴールまでのマスnを入力 int n = scan.nextInt(); int[] log = new int[10000]; int[][] route = new int[10000][2]; //道を記憶 int i=0; // int t=0; route[i][NUMBER] =1; route[i][COST] = 1; log[i] = 1; //System.out.printf("DBG>> Star\n"); boolean isEnd = false; while (!isEnd){ //移動量を出す(今のマス数を2進数にしたときの1の数) int move = Integer.bitCount(route[i][NUMBER]); //後退するマスを出す int val = route[i][NUMBER] - move; if(0 < val&&log[val-1]==0){ route[t+1][COST] = route[i][COST]+1; route[t+1][NUMBER] = val; log[route[t+1][NUMBER]-1] =1; t++; if(route[t][NUMBER] == n) isEnd = true; } //前進するマスを出す val = route[i][NUMBER] + move; if(val<=n&&log[val-1]==0){ route[t+1][COST] = route[i][COST]+1; route[t+1][NUMBER] = val; log[route[t+1][NUMBER]-1] =1; t++; if(route[t][NUMBER] == n) isEnd = true; } i++; if(t> Ref\n"); // for(int k=0;k<2;k++){ // for(int l=0;l<=t;l++) { // System.out.printf(" %2d", route[l][k]); // } // System.out.println(""); // } // for(int k=0;k> End\n"); if(t1){ here = route[here-1] - move; log[here-1] = 1; System.out.println(here); }else if(here+move