import numpy as np



N = int(eval(input()))





#@njit

def main(n):

    dp = []

    for i in range(10**5+1):

        dp.append(i)

    dp = np.array(dp)

    powers = [1]

    i = 1

    while True:

        if 6**i>10**5:

            break

        powers.append(6**i)

        if 9**i<=10**5:

            powers.append(9**i)

        i += 1

    powers.sort()

    for i in range(1,10**5+1):

        for j in range(len(powers)):

            if powers[j]>i:

                break

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



    print((dp[n]))            



if __name__=="__main__":

    main(N)
def getval():

    n = int(eval(input()))

    return n



def main(n):

    dp = [0]

    arr = [1]

    a1 = 6

    a2 = 9

    for i in range(1,100):

        if a1**i<=n:

            arr.append(a1**i)

        else:

            break

    for i in range(1,100):

        if a2**i<=n:

            arr.append(a2**i)

        else:

            break

    arr.sort()

    

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

        temp = i 

        for j in arr:

            if j>i:

                break

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

        dp.append(temp)



    print((dp[n]))



if __name__=="__main__":

    n = getval()

    main(n)
import numpy as np

N = int(eval(input()))
# @njit
def main(n):
    dp = []
    for i in range(10**5 + 1):
        dp.append(i)
    dp = np.array(dp)
    powers = [1]
    i = 1
    while True:
        if 6**i > 10**5:
            break
        powers.append(6**i)
        if 9**i <= 10**5:
            powers.append(9**i)
        i += 1
    powers.sort()
    for i in range(1, 10**5 + 1):
        for j in range(len(powers)):
            if powers[j] > i:
                break
            dp[i] = min(dp[i], dp[i - powers[j]] + 1)
    print((dp[n]))


if __name__ == "__main__":
    main(N)

def getval():
    n = int(eval(input()))
    return n


def main(n):
    dp = [0]
    arr = [1]
    a1 = 6
    a2 = 9
    for i in range(1, 100):
        if a1**i <= n:
            arr.append(a1**i)
        else:
            break
    for i in range(1, 100):
        if a2**i <= n:
            arr.append(a2**i)
        else:
            break
    arr.sort()
    for i in range(1, n + 1):
        temp = i
        for j in arr:
            if j > i:
                break
            temp = min(temp, dp[i - j] + 1)
        dp.append(temp)
    print((dp[n]))


if __name__ == "__main__":
    n = getval()
    main(n)

-import numpy as np +def getval(): +    n = int(eval(input())) +    return n -N = int(eval(input())) -# @njit + -    dp = [] -    for i in range(10**5 + 1): -        dp.append(i) -    dp = np.array(dp) -    powers = [1] -    i = 1 -    while True: -        if 6**i > 10**5: +    dp = [0] +    arr = [1] +    a1 = 6 +    a2 = 9 +    for i in range(1, 100): +        if a1**i <= n: +            arr.append(a1**i) +        else: -        powers.append(6**i) -        if 9**i <= 10**5: -            powers.append(9**i) -        i += 1 -    powers.sort() -    for i in range(1, 10**5 + 1): -        for j in range(len(powers)): -            if powers[j] > i: +    for i in range(1, 100): +        if a2**i <= n: +            arr.append(a2**i) +        else: +            break +    arr.sort() +    for i in range(1, n + 1): +        temp = i +        for j in arr: +            if j > i: -            dp[i] = min(dp[i], dp[i - powers[j]] + 1) +            temp = min(temp, dp[i - j] + 1) +        dp.append(temp) -    main(N) +    n = getval() +    main(n)