def bitget(a): s = str(bin(a)) n = len(s) counter = 0 for i in range(n): if s[i] == '1': counter += 1 return counter n = int(input()) table = [0 for i in range(n+1)] table[1] = 1 table[2] = 2 q = [2] next = 0 while len(q) != 0: if len(q) == 1: next = bitget(q[0]) if table[q[0]-next] == 0: table[q[0]-next] = table[q[0]]+1 q.append(q[0]-next) if q[0]+next <= n and table[q[0]+next] == 0: table[q[0]+next] = table[q[0]]+1 q.append(q[0]+next) q.pop(0) else: k = len(q) for i in range(k): next = bitget(q[i]) if table[q[i]-next] == 0: table[q[i]-next] = table[q[i]]+1 q.append(q[i]-next) if q[i]+next <= n and table[q[i]+next] == 0: table[q[i]+next] = table[q[i]]+1 q.append(q[i]+next) for i in range(k): q.pop(0) if table[n] != 0: print(table[n]) else: print('-1')