import sequtils,strutils,deques proc powInt(n : int64, m : int64, k = 1_000_000_007):int64 = if m == 0: return 1 elif m == 1: return (n mod k) if (m mod 2) == 0: return powInt((n*n) mod k,m div 2, k) mod k else: return (powInt((n*n) mod k,m div 2, k) * n) mod k proc p2(y:int):int = return powInt(2.int64,y.int64).int type bound = tuple[fro : int, to:int , times : int] var N = stdin.readline.parseInt A = newSeq[bound](0) miti,miti2 : bound Min_t = newSeqWith(N + 1,high(int) - 1000) masu : int n,m : int Min_t[1] = 0 miti2 = (0,1,1) A.add(miti2) m = 0 while A.len > m: masu = 0 miti = A[m] n = miti.to for i in 0..14: if (p2(i) and n) > 0: masu += 1 if n + masu <= N: if Min_t[n + masu] > miti.times + 1: Min_t[n + masu] = miti.times + 1 miti2 = (n,n + masu,miti.times + 1) A.add(miti2) if n - masu >= 1: if Min_t[n - masu] > miti.times + 1: Min_t[n - masu] = miti.times + 1 miti2 =(n,n - masu,miti.times + 1) A.add(miti2) m += 1 if Min_t[N] == high(int) - 1000: echo -1 else: echo Min_t[N]