n=int(eval(input()))



dp=[100001 for i in range(n+1)]

dp[0]=0



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

    p=1

    while p<=i:

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

        p*=6

    q=1

    while q<=i:

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

        q*=9



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

ans=10**100

for t in range(n+1):

    p=t

    q=n-p

    c=0

    while p>0:

        c+=p%6

        p=p//6

    while q>0:

        c+=q%9

        q=q//9

    ans=min(ans,c)

print(ans)













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

n = int(eval(input()))
ans = 10**100
for t in range(n + 1):
    p = t
    q = n - p
    c = 0
    while p > 0:
        c += p % 6
        p = p // 6
    while q > 0:
        c += q % 9
        q = q // 9
    ans = min(ans, c)
print(ans)

-dp = [100001 for i in range(n + 1)] -dp[0] = 0 -for i in range(1, n + 1): -    p = 1 -    while p <= i: -        dp[i] = min(dp[i], dp[i - p] + 1) -        p *= 6 -    q = 1 -    while q <= i: -        dp[i] = min(dp[i], dp[i - q] + 1) -        q *= 9 -print((dp[n])) +ans = 10**100 +for t in range(n + 1): +    p = t +    q = n - p +    c = 0 +    while p > 0: +        c += p % 6 +        p = p // 6 +    while q > 0: +        c += q % 9 +        q = q // 9 +    ans = min(ans, c) +print(ans)