n = int(eval(input()))

dp = [float('inf')] * 100010

dp[0] = 0

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

    power = 1

    while (power <= i):

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

        power *= 6

    power = 1

    while (power <= i):

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

        power *= 9

print((dp[n]))
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())))))
n = int(eval(input()))
dp = [float("inf")] * 100010
dp[0] = 0
for i in range(1, 100000 + 1):
    power = 1
    while power <= i:
        dp[i] = min(dp[i], dp[i - power] + 1)
        power *= 6
    power = 1
    while power <= i:
        dp[i] = min(dp[i], dp[i - power] + 1)
        power *= 9
print((dp[n]))

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())))))

-n = int(eval(input())) -dp = [float("inf")] * 100010 -dp[0] = 0 -for i in range(1, 100000 + 1): -    power = 1 -    while power <= i: -        dp[i] = min(dp[i], dp[i - power] + 1) -        power *= 6 -    power = 1 -    while power <= i: -        dp[i] = min(dp[i], dp[i - power] + 1) -        power *= 9 -print((dp[n])) +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())))))