n = int(eval(input()))

mn = 1e10



for i in range(n+1):

    ans = 0

    a, b = i, n-i



    while a>=6:

        a, mo = divmod(a, 6)

        ans += mo

    ans += a



    while b>=9:

        b, mo = divmod(b, 9)

        ans += mo

    ans += b



    mn = min(mn, ans)

print(mn)
def rec(n, x):

        d, m = divmod(n, x)

        if d: return m + rec(d, x)

        else: return m



ans = n = int(eval(input()))

for i in range(n+1):

        ans = min(ans, rec(i, 6)+rec(n-i, 9))

print(ans)
n = int(eval(input()))
mn = 1e10
for i in range(n + 1):
    ans = 0
    a, b = i, n - i
    while a >= 6:
        a, mo = divmod(a, 6)
        ans += mo
    ans += a
    while b >= 9:
        b, mo = divmod(b, 9)
        ans += mo
    ans += b
    mn = min(mn, ans)
print(mn)

def rec(n, x):
    d, m = divmod(n, x)
    if d:
        return m + rec(d, x)
    else:
        return m


ans = n = int(eval(input()))
for i in range(n + 1):
    ans = min(ans, rec(i, 6) + rec(n - i, 9))
print(ans)

-n = int(eval(input())) -mn = 1e10 +def rec(n, x): +    d, m = divmod(n, x) +    if d: +        return m + rec(d, x) +    else: +        return m + + +ans = n = int(eval(input())) -    ans = 0 -    a, b = i, n - i -    while a >= 6: -        a, mo = divmod(a, 6) -        ans += mo -    ans += a -    while b >= 9: -        b, mo = divmod(b, 9) -        ans += mo -    ans += b -    mn = min(mn, ans) -print(mn) +    ans = min(ans, rec(i, 6) + rec(n - i, 9)) +print(ans)