n=int(eval(input()))

def f(x):

    ans=0

    while x>0:

        ans+=x%6

        x=x//6

    return ans

def g(x):

    ans=0

    while x>0:

        ans+=x%9

        x=x//9

    return ans

ans=n

for i in range(n+1):

    ans=min(ans,f(i)+g(n-i))

print(ans)

n = int(eval(input()))



dp = [10**6 for _ in range(n+1)]

dp[0] = 0



for i in range(n+1):

    j6 = 1

    while i+j6<=n:

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

        j6 *= 6

    j9 = 1

    while i+j9<=n:

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

        j9 *= 9

print((dp[n]))

n = int(eval(input()))


def f(x):
    ans = 0
    while x > 0:
        ans += x % 6
        x = x // 6
    return ans


def g(x):
    ans = 0
    while x > 0:
        ans += x % 9
        x = x // 9
    return ans


ans = n
for i in range(n + 1):
    ans = min(ans, f(i) + g(n - i))
print(ans)

n = int(eval(input()))
dp = [10**6 for _ in range(n + 1)]
dp[0] = 0
for i in range(n + 1):
    j6 = 1
    while i + j6 <= n:
        dp[i + j6] = min(dp[i] + 1, dp[i + j6])
        j6 *= 6
    j9 = 1
    while i + j9 <= n:
        dp[i + j9] = min(dp[i] + 1, dp[i + j9])
        j9 *= 9
print((dp[n]))

- - -def f(x): -    ans = 0 -    while x > 0: -        ans += x % 6 -        x = x // 6 -    return ans - - -def g(x): -    ans = 0 -    while x > 0: -        ans += x % 9 -        x = x // 9 -    return ans - - -ans = n +dp = [10**6 for _ in range(n + 1)] +dp[0] = 0 -    ans = min(ans, f(i) + g(n - i)) -print(ans) +    j6 = 1 +    while i + j6 <= n: +        dp[i + j6] = min(dp[i] + 1, dp[i + j6]) +        j6 *= 6 +    j9 = 1 +    while i + j9 <= n: +        dp[i + j9] = min(dp[i] + 1, dp[i + j9]) +        j9 *= 9 +print((dp[n]))