import bisect as bi



li6 = [6 ** i for i in range(7)]

li9 = [9 ** i for i in range(6)]



def s69(n):

  if n < 6:

    return n

  else:

    s6 = li6[bi.bisect(li6, n)-1]

    s9 = li9[bi.bisect(li9, n)-1]

    return min(n // s6 + s69(n % s6), 1 + s69(n-s9), n // s9 + s69(n % s9))

  

print((s69(int(eval(input())))))
import bisect as bi



li6 = [6 ** i for i in range(7)]

li9 = [9 ** i for i in range(6)]



def s69(n):

  if n < 6:

    return n

  else:

    s6 = li6[bi.bisect(li6, n)-1]

    s9 = li9[bi.bisect(li9, n)-1]

    return min(n // s6 + s69(n % s6), 1 + s69(n-s9))

  

print((s69(int(eval(input())))))
import bisect as bi

li6 = [6**i for i in range(7)]
li9 = [9**i for i in range(6)]


def s69(n):
    if n < 6:
        return n
    else:
        s6 = li6[bi.bisect(li6, n) - 1]
        s9 = li9[bi.bisect(li9, n) - 1]
        return min(n // s6 + s69(n % s6), 1 + s69(n - s9), n // s9 + s69(n % s9))


print((s69(int(eval(input())))))

import bisect as bi

li6 = [6**i for i in range(7)]
li9 = [9**i for i in range(6)]


def s69(n):
    if n < 6:
        return n
    else:
        s6 = li6[bi.bisect(li6, n) - 1]
        s9 = li9[bi.bisect(li9, n) - 1]
        return min(n // s6 + s69(n % s6), 1 + s69(n - s9))


print((s69(int(eval(input())))))

-        return min(n // s6 + s69(n % s6), 1 + s69(n - s9), n // s9 + s69(n % s9)) +        return min(n // s6 + s69(n % s6), 1 + s69(n - s9))