N = int(input()) def wfs(): nowpos = {1:[1]} count = 1 while(True): count += 1 nextpos = {} for num in nowpos.keys(): progress = getBynum(num) if num + progress == N: return count elif (num + progress < N) & ((num + progress) not in nowpos[num]): nextpos[num + progress] = nowpos[num] + [num + progress] if num - progress == N: return count elif (num - progress > 1) & ((num - progress) not in nowpos[num]): nextpos[num - progress] = nowpos[num] + [num - progress] if(len(nextpos) == 0): return -1 nowpos = nextpos def getBynum(num): return bin(num)[2:].count('1') print(wfs())