#import collections

#aa = collections.Counter(a) # list to list

#from itertools import combinations # (string,3) 3回



mod = 10**9 + 7



def readInts():

  return list(map(int,input().split()))

def main():

    n = int(eval(input()))

    def count_(num,x):

        i = 0

        while num > 0:

            i += num % x

            num //= x

        return i

    ans = n



    for j in range(n + 1):

        ans = min(ans,count_(j,6)+count_(n-j,9))

    print(ans)









if __name__ == '__main__':

  main()

n = int(eval(input()))

dp = [n] * (n+1)

dp[0] = 0

# 貰うdp -- dp[i] に遷移を集める

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

    pow6 = 1

    while pow6 <= i: # i円を実現できるか、を回す

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

        pow6 *= 6

    pow9 = 1

    while pow9 <= i:

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

        pow9 *= 9

print((dp[n]))

# import collections
# aa = collections.Counter(a) # list to list
# from itertools import combinations # (string,3) 3回
mod = 10**9 + 7


def readInts():
    return list(map(int, input().split()))


def main():
    n = int(eval(input()))

    def count_(num, x):
        i = 0
        while num > 0:
            i += num % x
            num //= x
        return i

    ans = n
    for j in range(n + 1):
        ans = min(ans, count_(j, 6) + count_(n - j, 9))
    print(ans)


if __name__ == "__main__":
    main()

n = int(eval(input()))
dp = [n] * (n + 1)
dp[0] = 0
# 貰うdp -- dp[i] に遷移を集める
for i in range(1, n + 1):
    pow6 = 1
    while pow6 <= i:  # i円を実現できるか、を回す
        dp[i] = min(dp[i], dp[i - pow6] + 1)
        pow6 *= 6
    pow9 = 1
    while pow9 <= i:
        dp[i] = min(dp[i], dp[i - pow9] + 1)
        pow9 *= 9
print((dp[n]))

-# import collections -# aa = collections.Counter(a) # list to list -# from itertools import combinations # (string,3) 3回 -mod = 10**9 + 7 - - -def readInts(): -    return list(map(int, input().split())) - - -def main(): -    n = int(eval(input())) - -    def count_(num, x): -        i = 0 -        while num > 0: -            i += num % x -            num //= x -        return i - -    ans = n -    for j in range(n + 1): -        ans = min(ans, count_(j, 6) + count_(n - j, 9)) -    print(ans) - - -if __name__ == "__main__": -    main() +n = int(eval(input())) +dp = [n] * (n + 1) +dp[0] = 0 +# 貰うdp -- dp[i] に遷移を集める +for i in range(1, n + 1): +    pow6 = 1 +    while pow6 <= i:  # i円を実現できるか、を回す +        dp[i] = min(dp[i], dp[i - pow6] + 1) +        pow6 *= 6 +    pow9 = 1 +    while pow9 <= i: +        dp[i] = min(dp[i], dp[i - pow9] + 1) +        pow9 *= 9 +print((dp[n]))