l_6 = []

_ = 1

while 6**_ < 100000:

    l_6.append(6**_)

    _ += 1

l_6.sort(reverse = 1)



l_9= []

_ = 1

while 9**_ < 100000:

    l_9.append(9**_)

    _ += 1

l_9.sort(reverse = 1)



n = int(eval(input()))



w = 0

z = []

for i in range(n+1):

    x = i

    for j in range(len(l_6)):

        while l_6[j] <= x:

            x -= l_6[j]

            w += 1

    else:

        w += x

    

    y = n-i

    for k in range(len(l_9)):

        while l_9[k] <= y:

            y -= l_9[k]

            w += 1

    else:

        w += y



    z.append(w)

    w = 0



print((min(z)))

n = int(eval(input()))



dp = [0] + [100000]*n



ptn = [1]

_ = 1

while 6**_ < 100000:

    ptn.append(6**_)

    _ += 1



_ = 1

while 9**_ < 100000:

    ptn.append(9**_)

    _ += 1



for i in range(1,n+1):

    for j in ptn:

        if i-j >= 0:

            dp[i] = min(dp[i], dp[i-j]+1)

print((dp[-1]))
l_6 = []
_ = 1
while 6**_ < 100000:
    l_6.append(6**_)
    _ += 1
l_6.sort(reverse=1)
l_9 = []
_ = 1
while 9**_ < 100000:
    l_9.append(9**_)
    _ += 1
l_9.sort(reverse=1)
n = int(eval(input()))
w = 0
z = []
for i in range(n + 1):
    x = i
    for j in range(len(l_6)):
        while l_6[j] <= x:
            x -= l_6[j]
            w += 1
    else:
        w += x
    y = n - i
    for k in range(len(l_9)):
        while l_9[k] <= y:
            y -= l_9[k]
            w += 1
    else:
        w += y
    z.append(w)
    w = 0
print((min(z)))

n = int(eval(input()))
dp = [0] + [100000] * n
ptn = [1]
_ = 1
while 6**_ < 100000:
    ptn.append(6**_)
    _ += 1
_ = 1
while 9**_ < 100000:
    ptn.append(9**_)
    _ += 1
for i in range(1, n + 1):
    for j in ptn:
        if i - j >= 0:
            dp[i] = min(dp[i], dp[i - j] + 1)
print((dp[-1]))

-l_6 = [] +n = int(eval(input())) +dp = [0] + [100000] * n +ptn = [1] -    l_6.append(6**_) +    ptn.append(6**_) -l_6.sort(reverse=1) -l_9 = [] -    l_9.append(9**_) +    ptn.append(9**_) -l_9.sort(reverse=1) -n = int(eval(input())) -w = 0 -z = [] -for i in range(n + 1): -    x = i -    for j in range(len(l_6)): -        while l_6[j] <= x: -            x -= l_6[j] -            w += 1 -    else: -        w += x -    y = n - i -    for k in range(len(l_9)): -        while l_9[k] <= y: -            y -= l_9[k] -            w += 1 -    else: -        w += y -    z.append(w) -    w = 0 -print((min(z))) +for i in range(1, n + 1): +    for j in ptn: +        if i - j >= 0: +            dp[i] = min(dp[i], dp[i - j] + 1) +print((dp[-1]))