n=int(eval(input()))

s=[]

t=1000

p=0

def f9(n,p):

    while n>=9:

        n//=9

        p+=1

        f9(n,p)

    return p

def f6(n,p):

    while n>=6:

        n//=6

        p+=1

        f6(n,p)

    return p    

s.append((n,0))

while s:

    a=s[0][0]

    b=s[0][1]

    if a<6:

        t=min(t,b+a)

    if t>=b+1:

        p=0

        s.append((a-9**f9(a,p),b+1))

        p=0

        s.append((a-6**f6(a,p),b+1))

    s.pop(0)    

print(t)





N=int(eval(input()))

dp=[0]*(N+1)

def f(n):

    p=0

    q=0

    m=n

    while n>=1:

        n//=9

        p+=1

    while m>=1:

        m//=6

        q+=1

    s=[]

    for i in range(p):

        s.append(9**i)

    for j in range(q):    

        s.append(6**j)

    s.pop(0)    

    return s

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

    dp[i]=dp[i-1]+1

    for j in f(i):

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

print((dp[N]))        

        
n = int(eval(input()))
s = []
t = 1000
p = 0


def f9(n, p):
    while n >= 9:
        n //= 9
        p += 1
        f9(n, p)
    return p


def f6(n, p):
    while n >= 6:
        n //= 6
        p += 1
        f6(n, p)
    return p


s.append((n, 0))
while s:
    a = s[0][0]
    b = s[0][1]
    if a < 6:
        t = min(t, b + a)
    if t >= b + 1:
        p = 0
        s.append((a - 9 ** f9(a, p), b + 1))
        p = 0
        s.append((a - 6 ** f6(a, p), b + 1))
    s.pop(0)
print(t)

N = int(eval(input()))
dp = [0] * (N + 1)


def f(n):
    p = 0
    q = 0
    m = n
    while n >= 1:
        n //= 9
        p += 1
    while m >= 1:
        m //= 6
        q += 1
    s = []
    for i in range(p):
        s.append(9**i)
    for j in range(q):
        s.append(6**j)
    s.pop(0)
    return s


for i in range(1, N + 1):
    dp[i] = dp[i - 1] + 1
    for j in f(i):
        dp[i] = min(dp[i - j] + 1, dp[i])
print((dp[N]))

-n = int(eval(input())) -s = [] -t = 1000 -p = 0 +N = int(eval(input())) +dp = [0] * (N + 1) -def f9(n, p): -    while n >= 9: +def f(n): +    p = 0 +    q = 0 +    m = n +    while n >= 1: -        f9(n, p) -    return p +    while m >= 1: +        m //= 6 +        q += 1 +    s = [] +    for i in range(p): +        s.append(9**i) +    for j in range(q): +        s.append(6**j) +    s.pop(0) +    return s -def f6(n, p): -    while n >= 6: -        n //= 6 -        p += 1 -        f6(n, p) -    return p - - -s.append((n, 0)) -while s: -    a = s[0][0] -    b = s[0][1] -    if a < 6: -        t = min(t, b + a) -    if t >= b + 1: -        p = 0 -        s.append((a - 9 ** f9(a, p), b + 1)) -        p = 0 -        s.append((a - 6 ** f6(a, p), b + 1)) -    s.pop(0) -print(t) +for i in range(1, N + 1): +    dp[i] = dp[i - 1] + 1 +    for j in f(i): +        dp[i] = min(dp[i - j] + 1, dp[i]) +print((dp[N]))