def bit_num_sum(num): bit_num = bin(num)[2:] bit_num_str_list = list(str(bit_num)) bit_num_int_list = list(map(int, bit_num_str_list)) return sum(bit_num_int_list) N = int(input()) # dp[i] iに到達できる最短の移動数、不可能なら'-1' dp = [-1] * (N + 1) dp[1] = 1 for i in range(1, N + 1): ikisaki = i + bit_num_sum(i) if ikisaki <= N and dp[i] != -1: dp[ikisaki] = dp[i] + 1 modori = ikisaki - bit_num_sum(ikisaki) if 1 <= modori <= N and dp[modori] == -1: dp[modori] = dp[ikisaki] + 1 print(dp[N])