goal = int(input()) visited = {1:1} tsugi = {1:1} sonotsugi = {} while tsugi: if goal in visited: break for node in tsugi: p = node + int(bin(node).count("1")) m = node - int(bin(node).count("1")) if p not in visited and 0 < p < goal+1: visited.update({p:tsugi[node]+1}) sonotsugi.update({p:tsugi[node]+1}) if m not in visited and 0 < m < goal+1: visited.update({m:tsugi[node]+1}) sonotsugi.update({m:tsugi[node]+1}) tsugi = sonotsugi sonotsugi = {} print(visited[goal] if goal in visited else -1)