def min_coins(N): # 硬貨の額面を列挙 coins = set() i = 0 while (2 ** i) <= N: coins.add(2 ** i) i += 1 j = 0 while (3 ** j) <= N: coins.add(3 ** j) j += 1 coins = sorted(coins, reverse=True) def count_coins(amount): count = 0 for coin in coins: if amount == 0: break count += amount // coin amount %= coin return count min_count = float('inf') for X in range(N + 1): Y = X - N count_X = count_coins(X) count_Y = count_coins(abs(Y)) min_count = min(min_count, count_X + count_Y) return min_count # 入力の読み込み if __name__ == "__main__": import sys input = sys.stdin.read N = int(input().strip()) # 結果の計算 result = min_coins(N) # 結果の出力 print(result)