n=int(input()) def f(): lst = [0]*(n+1) lst[1] = 1 cnt = [1] while(cnt): num = cnt.pop(0) if num == n: return lst[num] step = str(bin(num))[2:].count("1") fowd_n = num + step back_n = num - step if fowd_n <= n and lst[fowd_n]==0: cnt.append(fowd_n) lst[fowd_n] =lst[num] + 1 if back_n > 0 and lst[back_n]==0: cnt.append(back_n) lst[back_n] =lst[num] + 1 return -1 print(f())