from collections import deque N = int(input()) t = [-1]*(N+1) t[1] = 1 q = deque() q.append(1) while q: crt = q.popleft() cnt = 0 a = crt while a: cnt += a&1 a >>= 1 p = crt+cnt m = crt-cnt if p <= N: if t[p] == -1: t[p] = t[crt] + 1 q.append(p) if m > 0: if t[m] == -1: t[m] = t[crt] + 1 q.append(m) print(t[N])