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()))



nine = [9**i for i in range(7, 0, -1)]

six = [6**i for i in range(8, 0, -1)]

tmp = {}



def f(n):

  if(n < 6):

    return n

  if(n in tmp):

    return tmp[n]

  

  m = 1e9+7

  if(n >= 9):

    mn = max(x for x in nine if x <= n)

    if(n%mn == 0):

      m = min(m, n//mn)

    else:

      m = min(m, f(n-mn)+1)

  ms = max(x for x in six if x <= n)

  if(n%ms == 0):

    m = min(m, n//ms)

  tmp[n] = min(m, f(n-ms)+1)

  return tmp[n]

print((f(N)))
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()))
nine = [9**i for i in range(7, 0, -1)]
six = [6**i for i in range(8, 0, -1)]
tmp = {}


def f(n):
    if n < 6:
        return n
    if n in tmp:
        return tmp[n]
    m = 1e9 + 7
    if n >= 9:
        mn = max(x for x in nine if x <= n)
        if n % mn == 0:
            m = min(m, n // mn)
        else:
            m = min(m, f(n - mn) + 1)
    ms = max(x for x in six if x <= n)
    if n % ms == 0:
        m = min(m, n // ms)
    tmp[n] = min(m, f(n - ms) + 1)
    return tmp[n]


print((f(N)))

-sixs = [6**i for i in range(1, 7)] -nines = [9**i for i in range(1, 6)] +nine = [9**i for i in range(7, 0, -1)] +six = [6**i for i in range(8, 0, -1)] -def dfs(n): +def f(n): -    m = 10**9 + 7 +    m = 1e9 + 7 -        max_nine = max(nine for nine in nines if nine <= n) -        if n % max_nine == 0: -            m = min(m, n // max_nine) +        mn = max(x for x in nine if x <= n) +        if n % mn == 0: +            m = min(m, n // mn) -            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) +            m = min(m, f(n - mn) + 1) +    ms = max(x for x in six if x <= n) +    if n % ms == 0: +        m = min(m, n // ms) +    tmp[n] = min(m, f(n - ms) + 1) -print((dfs(N))) +print((f(N)))