def funcDP(n):

    dp = [float('inf')] * (n + 10)

    dp[0] = 0

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

        for base in [6, 9]:

            powered = 1

            while powered <= i:

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

                powered *= base

    return dp[n]



if __name__ == '__main__':

    print((funcDP(int(eval(input())))))
def main():

    inf = 1 << 30



    N = int(eval(input()))



    dp = [inf] * (N + 1)

    dp[0] = 0



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

        t = inf

        x = 1

        while x <= i:

            t = min(t, dp[i - x])

            x *= 6

        x = 1

        while x <= i:

            t = min(t, dp[i - x])

            x *= 9

        dp[i] = t + 1



    print((dp[N]))





if __name__ == '__main__':

    main()

def funcDP(n):
    dp = [float("inf")] * (n + 10)
    dp[0] = 0
    for i in range(1, n + 1):
        for base in [6, 9]:
            powered = 1
            while powered <= i:
                dp[i] = min(dp[i], dp[i - powered] + 1)
                powered *= base
    return dp[n]


if __name__ == "__main__":
    print((funcDP(int(eval(input())))))

def main():
    inf = 1 << 30
    N = int(eval(input()))
    dp = [inf] * (N + 1)
    dp[0] = 0
    for i in range(1, N + 1):
        t = inf
        x = 1
        while x <= i:
            t = min(t, dp[i - x])
            x *= 6
        x = 1
        while x <= i:
            t = min(t, dp[i - x])
            x *= 9
        dp[i] = t + 1
    print((dp[N]))


if __name__ == "__main__":
    main()

-def funcDP(n): -    dp = [float("inf")] * (n + 10) +def main(): +    inf = 1 << 30 +    N = int(eval(input())) +    dp = [inf] * (N + 1) -    for i in range(1, n + 1): -        for base in [6, 9]: -            powered = 1 -            while powered <= i: -                dp[i] = min(dp[i], dp[i - powered] + 1) -                powered *= base -    return dp[n] +    for i in range(1, N + 1): +        t = inf +        x = 1 +        while x <= i: +            t = min(t, dp[i - x]) +            x *= 6 +        x = 1 +        while x <= i: +            t = min(t, dp[i - x]) +            x *= 9 +        dp[i] = t + 1 +    print((dp[N])) -    print((funcDP(int(eval(input()))))) +    main()