N = input() been = [-1] + [1] + [-1] * N locations = [1] while locations: loc = locations.pop(0) width = bin(loc).count('1') move_r, move_l = loc + width, loc-width if move_r <= N: if been[move_r] == -1: been[move_r] = been[loc] + 1 locations.append(move_r) if move_l >= 1 and been[move_l] == -1: been[move_l] = been[loc] + 1 locations.append(move_l) if been[N] != -1: print been[N] exit() print -1