n = int(eval(input()))



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

dp[0] = 0

for i in range(1,100001):

    p = 1

    while p <= i:

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

        p *= 6

    

    p = 1

    while p <= i:

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

        p *= 9



print((dp[n]))
n = int(eval(input()))

minC = n

for i in range(n+1):

    cnt = 0

    t = i

    while t > 0:

        cnt += t%6

        t //= 6

    t = n-i

    

    while t > 0:

        cnt += t%9

        t //= 9

        

    if minC > cnt:

        minC = cnt

print(minC)
n = int(eval(input()))
dp = [float("inf")] * 100001
dp[0] = 0
for i in range(1, 100001):
    p = 1
    while p <= i:
        dp[i] = min(dp[i], dp[i - p] + 1)
        p *= 6
    p = 1
    while p <= i:
        dp[i] = min(dp[i], dp[i - p] + 1)
        p *= 9
print((dp[n]))

n = int(eval(input()))
minC = n
for i in range(n + 1):
    cnt = 0
    t = i
    while t > 0:
        cnt += t % 6
        t //= 6
    t = n - i
    while t > 0:
        cnt += t % 9
        t //= 9
    if minC > cnt:
        minC = cnt
print(minC)

-dp = [float("inf")] * 100001 -dp[0] = 0 -for i in range(1, 100001): -    p = 1 -    while p <= i: -        dp[i] = min(dp[i], dp[i - p] + 1) -        p *= 6 -    p = 1 -    while p <= i: -        dp[i] = min(dp[i], dp[i - p] + 1) -        p *= 9 -print((dp[n])) +minC = n +for i in range(n + 1): +    cnt = 0 +    t = i +    while t > 0: +        cnt += t % 6 +        t //= 6 +    t = n - i +    while t > 0: +        cnt += t % 9 +        t //= 9 +    if minC > cnt: +        minC = cnt +print(minC)