N = int(eval(input()))



ans = 10**9+7

for i in range(N+1):

  j = N-i

  cnt = 0

  while i > 0:

    cnt += i%6

    i //= 6

  while j > 0:

    cnt += j%9

    j //= 9

  ans = min(ans, cnt)

print(ans)
N = int(eval(input()))



sixs = [6**i for i in range(1,7)]

nines = [9**i for i in range(1,6)]

tmp = {}



def dfs(n):

  if(n < 6):

    return n



  if(n in tmp):

    return tmp[n]



  m = 10**9+7

  if(n >= 9):

    max_nine = max(nine for nine in nines if nine <= n)

    if(n%max_nine == 0):

      m = min(m, n // max_nine)

    else:

      m = min(m, dfs(n - max_nine) + 1)



  max_six = max(six for six in sixs if six <= n)

  if(n%max_six == 0):

    m = min(m, n // max_six)



  tmp[n] = min(m, dfs(n - max_six) + 1)

  return tmp[n]



print((dfs(N)))
N = int(eval(input()))
ans = 10**9 + 7
for i in range(N + 1):
    j = N - i
    cnt = 0
    while i > 0:
        cnt += i % 6
        i //= 6
    while j > 0:
        cnt += j % 9
        j //= 9
    ans = min(ans, cnt)
print(ans)

N = int(eval(input()))
sixs = [6**i for i in range(1, 7)]
nines = [9**i for i in range(1, 6)]
tmp = {}


def dfs(n):
    if n < 6:
        return n
    if n in tmp:
        return tmp[n]
    m = 10**9 + 7
    if n >= 9:
        max_nine = max(nine for nine in nines if nine <= n)
        if n % max_nine == 0:
            m = min(m, n // max_nine)
        else:
            m = min(m, dfs(n - max_nine) + 1)
    max_six = max(six for six in sixs if six <= n)
    if n % max_six == 0:
        m = min(m, n // max_six)
    tmp[n] = min(m, dfs(n - max_six) + 1)
    return tmp[n]


print((dfs(N)))

-ans = 10**9 + 7 -for i in range(N + 1): -    j = N - i -    cnt = 0 -    while i > 0: -        cnt += i % 6 -        i //= 6 -    while j > 0: -        cnt += j % 9 -        j //= 9 -    ans = min(ans, cnt) -print(ans) +sixs = [6**i for i in range(1, 7)] +nines = [9**i for i in range(1, 6)] +tmp = {} + + +def dfs(n): +    if n < 6: +        return n +    if n in tmp: +        return tmp[n] +    m = 10**9 + 7 +    if n >= 9: +        max_nine = max(nine for nine in nines if nine <= n) +        if n % max_nine == 0: +            m = min(m, n // max_nine) +        else: +            m = min(m, dfs(n - max_nine) + 1) +    max_six = max(six for six in sixs if six <= n) +    if n % max_six == 0: +        m = min(m, n // max_six) +    tmp[n] = min(m, dfs(n - max_six) + 1) +    return tmp[n] + + +print((dfs(N)))