import collections N = int(input()) lscost = [float('INF')]*(N+1) used = [False]*(N+1) lscost[1] = 1 d = collections.deque() d.append(1) while d: v = d.popleft() if used[v]: continue used[v] = True bitlen = bin(v).count('1') g = [v-bitlen,v+bitlen] for j in g: if j > N: continue if lscost[j] > lscost[v]+1: lscost[j] = lscost[v]+1 d.append(j) if lscost[N] == float('INF'): print(-1) else: print(lscost[N])