l9 = []

l9.append(1)

tmp = 1

while tmp <= 10**5:

  tmp *= 9

  l9.append(tmp)

l6 = []

l6.append(1)

tmp = 1

while tmp <= 10**5:

  tmp *= 6

  l6.append(tmp)



  

import bisect 

n = int(eval(input()))

ans = 10**6



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



  a = i

  b = n - i

  p = 0

  while a > 0:

    idx = bisect.bisect_left(l6, a)

    if a == l6[idx]:

      a -= l6[idx]

    else:

      a -= l6[idx-1]

    p += 1

    

  q = 0

  while b > 0:

    idx = bisect.bisect_left(l9, b)

    if b == l9[idx]:

      b -= l9[idx]

    else:

      b -= l9[idx-1]

    q += 1

    

  ans = min(ans, p+q)



print(ans)
import bisect

N = int(eval(input()))



ans = N

sixs = [1]

i = 6

while i <= N:

    sixs.append(i)

    i *= 6

nines = [1]

i = 9

while i <= N:

    nines.append(i)

    i *= 9



for i in range(N+1):

    #iの分だけ6**xで構成

    cnt = 0

    a = i

    b = N-i

    while a > 0:

        idx = bisect.bisect_right(sixs, a)

        cnt += a // sixs[idx-1]

        a -= (a // sixs[idx-1]) * sixs[idx-1]



    while b > 0:

        idx = bisect.bisect_right(nines, b)

        cnt += b // nines[idx-1]

        b -= (b // nines[idx-1]) * nines[idx-1]



    ans = min(ans, cnt)



print(ans)
l9 = []
l9.append(1)
tmp = 1
while tmp <= 10**5:
    tmp *= 9
    l9.append(tmp)
l6 = []
l6.append(1)
tmp = 1
while tmp <= 10**5:
    tmp *= 6
    l6.append(tmp)
import bisect

n = int(eval(input()))
ans = 10**6
for i in range(0, n + 1):
    a = i
    b = n - i
    p = 0
    while a > 0:
        idx = bisect.bisect_left(l6, a)
        if a == l6[idx]:
            a -= l6[idx]
        else:
            a -= l6[idx - 1]
        p += 1
    q = 0
    while b > 0:
        idx = bisect.bisect_left(l9, b)
        if b == l9[idx]:
            b -= l9[idx]
        else:
            b -= l9[idx - 1]
        q += 1
    ans = min(ans, p + q)
print(ans)

import bisect

N = int(eval(input()))
ans = N
sixs = [1]
i = 6
while i <= N:
    sixs.append(i)
    i *= 6
nines = [1]
i = 9
while i <= N:
    nines.append(i)
    i *= 9
for i in range(N + 1):
    # iの分だけ6**xで構成
    cnt = 0
    a = i
    b = N - i
    while a > 0:
        idx = bisect.bisect_right(sixs, a)
        cnt += a // sixs[idx - 1]
        a -= (a // sixs[idx - 1]) * sixs[idx - 1]
    while b > 0:
        idx = bisect.bisect_right(nines, b)
        cnt += b // nines[idx - 1]
        b -= (b // nines[idx - 1]) * nines[idx - 1]
    ans = min(ans, cnt)
print(ans)

-l9 = [] -l9.append(1) -tmp = 1 -while tmp <= 10**5: -    tmp *= 9 -    l9.append(tmp) -l6 = [] -l6.append(1) -tmp = 1 -while tmp <= 10**5: -    tmp *= 6 -    l6.append(tmp) -n = int(eval(input())) -ans = 10**6 -for i in range(0, n + 1): +N = int(eval(input())) +ans = N +sixs = [1] +i = 6 +while i <= N: +    sixs.append(i) +    i *= 6 +nines = [1] +i = 9 +while i <= N: +    nines.append(i) +    i *= 9 +for i in range(N + 1): +    # iの分だけ6**xで構成 +    cnt = 0 -    b = n - i -    p = 0 +    b = N - i -        idx = bisect.bisect_left(l6, a) -        if a == l6[idx]: -            a -= l6[idx] -        else: -            a -= l6[idx - 1] -        p += 1 -    q = 0 +        idx = bisect.bisect_right(sixs, a) +        cnt += a // sixs[idx - 1] +        a -= (a // sixs[idx - 1]) * sixs[idx - 1] -        idx = bisect.bisect_left(l9, b) -        if b == l9[idx]: -            b -= l9[idx] -        else: -            b -= l9[idx - 1] -        q += 1 -    ans = min(ans, p + q) +        idx = bisect.bisect_right(nines, b) +        cnt += b // nines[idx - 1] +        b -= (b // nines[idx - 1]) * nines[idx - 1] +    ans = min(ans, cnt)