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


N, = readints()
DEPTH_LIMIT = 900
INF = 10**9
memo = [None]*(N+1)
memo[0] = 0


def solve(n, depth):
    global INF
    global DEPTH_LIMIT
    global memo
    if DEPTH_LIMIT < depth:
        return INF
    # print(n, depth)
    if memo[n] != None:
        return memo[n]
    ret = INF
    i = 1
    while i <= n:
        ret = min(ret, solve(n-i, depth+1)+1)
        i *= 6
    i = 1
    while i <= n:
        ret = min(ret, solve(n-i, depth+1)+1)
        i *= 9
    memo[n] = ret
    return memo[n]


print(solve(N, 0))
