#include #include #define N 10001 struct MASU{ int n, f, k; }bord[N]; struct MYQ{ void *current; struct MYQ *next; struct MYQ *back; }; struct MYQ *head, *tail; void ini(void) { head = tail = NULL; return; } void end(void) { struct MYQ *h, *w; for(h = head; h != NULL; h = w){ w = h->next; free(h); } return; } struct MYQ* newmyq(void *current) { struct MYQ *new; new = malloc(sizeof(struct MYQ)); new->next = new->back = NULL; new->current = current; return(new); } void enque(struct MYQ *q) { if(tail == NULL){ head = tail = q; }else{ q->next = head; head->back = q; head = q; } return; } struct MYQ* deque(void) { struct MYQ *w; if(tail == NULL){ return(NULL); } w = tail; tail = tail->back; return(w); } void qprint(void) { struct MYQ *h, *t; struct MASU *w; for(h = head; h != NULL; h = h->next){ w = h->current; printf("%d", w->n); } printf("\n"); for(t = tail; t!= NULL; t = t->back){ w = t->current; printf("%d", w->n); } printf("\n"); } int bit(int n) { int bit = 0; do{ if(n % 2 == 1){ bit++; } }while(n >>= 1); return(bit); } int main(int argc, const char * argv[]) { int n, i, front, back; struct MYQ *w; struct MASU *b; ini(); scanf("%d", &n); for(i = 1; i <= n; i++){ bord[i].n = i; bord[i].f = 0; bord[i].k = 0; } enque(newmyq(&bord[1])); bord[1].f = bord[1].k = 1; while((w = deque()) != NULL){ b = w->current; front = b->n + bit(b->n); if(front <= n && bord[front].f == 0){ enque(newmyq(&bord[front])); bord[front].f = 1; bord[front].k = bord[b->n].k + 1; } back = b->n - bit(b->n); if(1 <= back && bord[back].f == 0){ enque(newmyq(&bord[back])); bord[back].f = 1; bord[back].k = bord[b->n].k + 1; } } if(0 < bord[n].k){ printf("%d\n", bord[n].k); }else{ printf("%d\n", -1); } end(); return 0; }