

n = int(eval(input()))

dp = [10**9]*(n+1)

L = [6**i for i in range(1,7)]

L += [9**i for i in range(1,6)]



dp[0] = 0



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

    dp[i] = dp[i-1]+1

    k = 6

    while i-k >=0:

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

        k *=6

    k = 9

    while i-k >=0:

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

        k *=9

    #print(dp)

print((dp[n]))





n = int(eval(input()))



dp = [0]*(n+10)



for i in range(n+1):

    dp[i+1] = dp[i] +1

    



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

    k = 6

    while k <=n:

        if i-k>=0:

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

            #print(dp[i-1],dp[i-k])

        k *=6



        #print(dp)

    k = 9

    while k <=n:

        if i-k>=0:

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

           

        k *=9



print((dp[n]))
n = int(eval(input()))
dp = [10**9] * (n + 1)
L = [6**i for i in range(1, 7)]
L += [9**i for i in range(1, 6)]
dp[0] = 0
for i in range(1, n + 1):
    dp[i] = dp[i - 1] + 1
    k = 6
    while i - k >= 0:
        dp[i] = min(dp[i], dp[i - k] + 1)
        k *= 6
    k = 9
    while i - k >= 0:
        dp[i] = min(dp[i], dp[i - k] + 1)
        k *= 9
    # print(dp)
print((dp[n]))

n = int(eval(input()))
dp = [0] * (n + 10)
for i in range(n + 1):
    dp[i + 1] = dp[i] + 1
for i in range(1, n + 1):
    k = 6
    while k <= n:
        if i - k >= 0:
            dp[i] = min(dp[i], dp[i - k] + 1)
            # print(dp[i-1],dp[i-k])
        k *= 6
        # print(dp)
    k = 9
    while k <= n:
        if i - k >= 0:
            dp[i] = min(dp[i], dp[i - k] + 1)
        k *= 9
print((dp[n]))

-dp = [10**9] * (n + 1) -L = [6**i for i in range(1, 7)] -L += [9**i for i in range(1, 6)] -dp[0] = 0 +dp = [0] * (n + 10) +for i in range(n + 1): +    dp[i + 1] = dp[i] + 1 -    dp[i] = dp[i - 1] + 1 -    while i - k >= 0: -        dp[i] = min(dp[i], dp[i - k] + 1) +    while k <= n: +        if i - k >= 0: +            dp[i] = min(dp[i], dp[i - k] + 1) +            # print(dp[i-1],dp[i-k]) +        # print(dp) -    while i - k >= 0: -        dp[i] = min(dp[i], dp[i - k] + 1) +    while k <= n: +        if i - k >= 0: +            dp[i] = min(dp[i], dp[i - k] + 1) -    # print(dp)