def nbasesum(value,n):

    nbasevalue = 0

    while value > 0:

        nbasevalue += value%n

        value //= n

    return nbasevalue



N = int(eval(input()))

ans = nbasesum(N,9)



for i in range(N+1):

    c6 = i

    c9 = N-i

    ans = min(ans, nbasesum(c9,9)+nbasesum(c6,6))



print(ans)

n = int(eval(input()))

m = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]

dp = [10**6 for i in range(100001)]

for mi in m:

    dp[mi] = 1

for i in range(1,100001):

    for mi in m:

        if i+mi <= 100000:

            dp[i+mi] = min(dp[i+mi],dp[i]+1)

        else:

            break

print((dp[n]))

def nbasesum(value, n):
    nbasevalue = 0
    while value > 0:
        nbasevalue += value % n
        value //= n
    return nbasevalue


N = int(eval(input()))
ans = nbasesum(N, 9)
for i in range(N + 1):
    c6 = i
    c9 = N - i
    ans = min(ans, nbasesum(c9, 9) + nbasesum(c6, 6))
print(ans)

n = int(eval(input()))
m = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
dp = [10**6 for i in range(100001)]
for mi in m:
    dp[mi] = 1
for i in range(1, 100001):
    for mi in m:
        if i + mi <= 100000:
            dp[i + mi] = min(dp[i + mi], dp[i] + 1)
        else:
            break
print((dp[n]))

-def nbasesum(value, n): -    nbasevalue = 0 -    while value > 0: -        nbasevalue += value % n -        value //= n -    return nbasevalue - - -N = int(eval(input())) -ans = nbasesum(N, 9) -for i in range(N + 1): -    c6 = i -    c9 = N - i -    ans = min(ans, nbasesum(c9, 9) + nbasesum(c6, 6)) -print(ans) +n = int(eval(input())) +m = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049] +dp = [10**6 for i in range(100001)] +for mi in m: +    dp[mi] = 1 +for i in range(1, 100001): +    for mi in m: +        if i + mi <= 100000: +            dp[i + mi] = min(dp[i + mi], dp[i] + 1) +        else: +            break +print((dp[n]))