#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from functools import lru_cache popcount = [0] for _ in range(10): popcount = popcount + [x + 1 for x in popcount] @lru_cache(None) def simu(B, n, add): assert n < (1 << B) if B < 10: step = 0 while n < (1 << B): n += popcount[n] + add step += 1 return step, n step, n = simu(B - 1, n, add) m = n - (1 << (B - 1)) step1, n = simu(B - 1, m, add + 1) n += 1 << (B - 1) return step + step1, n def simu_naive(B, n, add): step = 0 while n < 1 << B: n += bin(n).count('1') + add step += 1 return step, n N = int(read()) add = 0 total_step = 1 n = 1 for B in range(60, 10, -1): if N - n < (1 << B): continue q, r = divmod(n, 1 << B) step, next_n = simu(B, r, bin(q).count('1')) n = (q << B) + next_n total_step += step while n < N: total_step += 1 n += bin(n).count('1') answer = -1 if n != N else total_step print(answer)