import strutils, sequtils, sets proc trfm(i: int): int = i.toBin(16).count('1') let N = stdin.readLine.parseInt var arr = newSeq[int](N) ttl_S, mini_S1, mini_S2 = initSet[int]() x = 1 mini_S1.incl 0 arr[0] = 1 proc go(x: int) = for i0 in mini_S1: var i = i0 while true: let pre = i i += (i + 1).trfm * x if x == 1: if i >= N: break else: if i <= 0: break arr[i] = arr[pre] + 1 if i notin ttl_S: mini_S2.incl i ttl_S.incl i if mini_S2.len == 0: echo -1; quit() swap(mini_S1, mini_S2) mini_S2.clear while arr[^1] == 0: go(x) x *= -1 echo arr[^1]