#!/usr/bin/env python3 from collections import defaultdict def memoize(f): table = {} def func(*args): if not args in table: table[args] = f(*args) return table[args] return func @memoize def nexts(node): r = sum([int(x) for x in list(format(node[0], "b"))]) nxts = [i + node[0] for i in [-r, r] if 0 < i + node[0] <= N] return zip(nxts, [node[1] + 1] * len(nxts)) def print_route(route, node): while node > 0: print(node) node = route[node] N = int(input()) node = (1, 1, 0) pend = [] went = [] route = {} while node[0] < N: went.append(node[0]) pend.extend([n for n in nexts(node) if n[0] not in went]) if len(pend) == 0: print(-1) exit() node = pend.pop(0) print(node[1])