# 貪欲的だが「6^i円で払う分」と「9^i円で払う分」にNを分割してN+1通り全探索．





def how_many_times_withdraw(withdraw, base, limit):

    result = 0

    while withdraw >= base:

        result += withdraw // (base ** limit)

        withdraw = withdraw % (base ** limit)

        limit -= 1

    return result + withdraw





def main():

    N = int(eval(input()))

    limit = []

    answer = float("inf")

    for yen in [6, 9]:

        i = 1

        while yen ** i <= N:

            i += 1

        limit.append(i - 1)

    for i in range(N + 1):

        answer = min(answer, how_many_times_withdraw(i, 6, limit[0]) + how_many_times_withdraw(N - i, 9, limit[1]))

    print(answer)





if __name__ == '__main__':

    main()
def make_list(a, n):

    res = [1]

    i = 1

    while pow(a, i) <= n:

        res.append(pow(a, i))

        i += 1

    return res[::-1]





def find_withdraw_times(n, w):

    res = 0

    for ww in w:

        res += n // ww

        n %= ww

    return res





def main():

    n = int(eval(input()))

    answer = float("inf")

    way_6 = make_list(6, n)

    way_9 = make_list(9, n)

    for i in range(n + 1):

        answer = min(answer, find_withdraw_times(i, way_6) + find_withdraw_times(n - i, way_9))

    print(answer)





if __name__ == '__main__':

    main()



# 貪欲的だが「6^i円で払う分」と「9^i円で払う分」にNを分割してN+1通り全探索．
def how_many_times_withdraw(withdraw, base, limit):
    result = 0
    while withdraw >= base:
        result += withdraw // (base**limit)
        withdraw = withdraw % (base**limit)
        limit -= 1
    return result + withdraw


def main():
    N = int(eval(input()))
    limit = []
    answer = float("inf")
    for yen in [6, 9]:
        i = 1
        while yen**i <= N:
            i += 1
        limit.append(i - 1)
    for i in range(N + 1):
        answer = min(
            answer,
            how_many_times_withdraw(i, 6, limit[0])
            + how_many_times_withdraw(N - i, 9, limit[1]),
        )
    print(answer)


if __name__ == "__main__":
    main()

def make_list(a, n):
    res = [1]
    i = 1
    while pow(a, i) <= n:
        res.append(pow(a, i))
        i += 1
    return res[::-1]


def find_withdraw_times(n, w):
    res = 0
    for ww in w:
        res += n // ww
        n %= ww
    return res


def main():
    n = int(eval(input()))
    answer = float("inf")
    way_6 = make_list(6, n)
    way_9 = make_list(9, n)
    for i in range(n + 1):
        answer = min(
            answer, find_withdraw_times(i, way_6) + find_withdraw_times(n - i, way_9)
        )
    print(answer)


if __name__ == "__main__":
    main()

-# 貪欲的だが「6^i円で払う分」と「9^i円で払う分」にNを分割してN+1通り全探索． -def how_many_times_withdraw(withdraw, base, limit): -    result = 0 -    while withdraw >= base: -        result += withdraw // (base**limit) -        withdraw = withdraw % (base**limit) -        limit -= 1 -    return result + withdraw +def make_list(a, n): +    res = [1] +    i = 1 +    while pow(a, i) <= n: +        res.append(pow(a, i)) +        i += 1 +    return res[::-1] + + +def find_withdraw_times(n, w): +    res = 0 +    for ww in w: +        res += n // ww +        n %= ww +    return res -    N = int(eval(input())) -    limit = [] +    n = int(eval(input())) -    for yen in [6, 9]: -        i = 1 -        while yen**i <= N: -            i += 1 -        limit.append(i - 1) -    for i in range(N + 1): +    way_6 = make_list(6, n) +    way_9 = make_list(9, n) +    for i in range(n + 1): -            answer, -            how_many_times_withdraw(i, 6, limit[0]) -            + how_many_times_withdraw(N - i, 9, limit[1]), +            answer, find_withdraw_times(i, way_6) + find_withdraw_times(n - i, way_9)