N = int(eval(input()))

n6 = 1

n9 = 1

li = []

while 9**n9 <= N:

    li.append(9**n9)

    n9 += 1

while 6**n6 <= N:

    li.append(6**n6)

    n6 += 1



li.sort()

INF = 10**10

# 1円の場合はね

dp = [[i for i in range(N+1)] for i in range(len(li) + 1)]

dp = [i for i in range(N+1)]





for i,l in enumerate(li):

    for j in range(N+1):

        if j+l > N : continue

        dp[j+l] = min(dp[j+l], dp[j]+1)

print((dp[N]))

N = int(eval(input()))



array = []

cnt6 = 0

while 6**cnt6 <= N:

    array.append(6**cnt6)

    cnt6 += 1

    

cnt9 = 0

while 9**cnt9 <= N:

    array.append(9**cnt9)

    cnt9 += 1



a = sorted(list(set(array)))

# 最小

dp = [i for i in range(N+1)]

# print(a)

n = 6

for n in a:

    for i in range(N+1):

        if i+n <= N:

            dp[i+n] = min(dp[i]+1, dp[i+n])

print((dp[N]))

N = int(eval(input()))
n6 = 1
n9 = 1
li = []
while 9**n9 <= N:
    li.append(9**n9)
    n9 += 1
while 6**n6 <= N:
    li.append(6**n6)
    n6 += 1
li.sort()
INF = 10**10
# 1円の場合はね
dp = [[i for i in range(N + 1)] for i in range(len(li) + 1)]
dp = [i for i in range(N + 1)]
for i, l in enumerate(li):
    for j in range(N + 1):
        if j + l > N:
            continue
        dp[j + l] = min(dp[j + l], dp[j] + 1)
print((dp[N]))

N = int(eval(input()))
array = []
cnt6 = 0
while 6**cnt6 <= N:
    array.append(6**cnt6)
    cnt6 += 1
cnt9 = 0
while 9**cnt9 <= N:
    array.append(9**cnt9)
    cnt9 += 1
a = sorted(list(set(array)))
# 最小
dp = [i for i in range(N + 1)]
# print(a)
n = 6
for n in a:
    for i in range(N + 1):
        if i + n <= N:
            dp[i + n] = min(dp[i] + 1, dp[i + n])
print((dp[N]))

-n6 = 1 -n9 = 1 -li = [] -while 9**n9 <= N: -    li.append(9**n9) -    n9 += 1 -while 6**n6 <= N: -    li.append(6**n6) -    n6 += 1 -li.sort() -INF = 10**10 -# 1円の場合はね -dp = [[i for i in range(N + 1)] for i in range(len(li) + 1)] +array = [] +cnt6 = 0 +while 6**cnt6 <= N: +    array.append(6**cnt6) +    cnt6 += 1 +cnt9 = 0 +while 9**cnt9 <= N: +    array.append(9**cnt9) +    cnt9 += 1 +a = sorted(list(set(array))) +# 最小 -for i, l in enumerate(li): -    for j in range(N + 1): -        if j + l > N: -            continue -        dp[j + l] = min(dp[j + l], dp[j] + 1) +# print(a) +n = 6 +for n in a: +    for i in range(N + 1): +        if i + n <= N: +            dp[i + n] = min(dp[i] + 1, dp[i + n])