using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Remoting.Contexts;

namespace Contest
{
    class Scanner
    {
        private string[] line = new string[0];
        private int index = 0;

        public string Next()
        {
            if (line.Length <= index)
            {
                line = Console.ReadLine().Split(' ');
                index = 0;
            }

            var res = line[index];
            index++;
            return res;
        }

        public int NextInt()
        {
            return int.Parse(Next());
        }

        public long NextLong()
        {
            return long.Parse(Next());
        }

        public string[] Array()
        {
            line = Console.ReadLine().Split(' ');
            index = line.Length;
            return line;
        }

        public int[] IntArray()
        {
            var array = Array();
            var result = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = int.Parse(array[i]);
            }

            return result;
        }

        public long[] LongArray()
        {
            var array = Array();
            var result = new long[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = long.Parse(array[i]);
            }

            return result;
        }
    }

    class Program
    {
        private int N;
        private void Scan()
        {
            var sc = new Scanner();
            N = sc.NextInt();
        }


        public void Solve()
        {
            Scan();
            List<int> l = new List<int>();
            l.Add(1);
            int six = 1;
            for (int i = 0; i < 6; i++)
            {
                six *= 6;
                l.Add(six);
            }

            int nine = 1;
            for (int i = 0; i < 5; i++)
            {
                nine *= 9;
                l.Add(nine);
            }

            var dp = (new int[100001]).Select(i => 999999999).ToArray();
            dp[0] = 0;
            for (int i = 0; i <= 100000; i++)
            {
                foreach (var j in l)
                {
                    if (i + j <= 100000)
                    {
                        dp[i + j] = Math.Min(dp[i + j], dp[i] + 1);
                    }
                }
            }
            Console.WriteLine(dp[N]);
        }


        static void Main() => new Program().Solve();
    }

    static class Algorithm
    {
        public static int LowerBound<T>(T[] array, T target, int begin, int end, Comparison<T> comparison)
        {
            while (begin < end)
            {
                int mid = ((end - begin) >> 1) + begin;
                if (comparison(array[mid], target) < 0)
                {
                    begin = mid + 1;
                }
                else
                {
                    end = mid;
                }
            }

            return begin;
        }
        public static int LowerBound<T>(T[] array, T target, int begin, int end, IComparer<T> comparer) => LowerBound(array, target, begin, end, comparer.Compare);
        public static int LowerBound<T>(T[] array, T target, int begin, int end) => LowerBound(array, target, begin, end, Comparer<T>.Default);
        public static int LowerBound<T>(T[] array, T target, Comparison<T> comparison) => LowerBound(array, target, 0, array.Length, comparison);
        public static int LowerBound<T>(T[] array, T target, IComparer<T> comparer) => LowerBound(array, target, 0, array.Length, comparer);
        public static int LowerBound<T>(T[] array, T target) => LowerBound(array, target, 0, array.Length);

        public static int UpperBound<T>(T[] array, T target, int begin, int end, Comparison<T> comparison)
        {
            while (begin < end)
            {
                int mid = ((end - begin) >> 1) + begin;
                if (comparison(array[mid], target) <= 0)
                {
                    begin = mid + 1;
                }
                else
                {
                    end = mid;
                }
            }

            return begin;
        }
        public static int UpperBound<T>(T[] array, T target, int begin, int end, IComparer<T> comparer) => UpperBound(array, target, begin, end, comparer.Compare);
        public static int UpperBound<T>(T[] array, T target, int begin, int end) => UpperBound(array, target, begin, end, Comparer<T>.Default);
        public static int UpperBound<T>(T[] array, T target, Comparison<T> comparison) => UpperBound(array, target, 0, array.Length, comparison);
        public static int UpperBound<T>(T[] array, T target, IComparer<T> comparer) => UpperBound(array, target, 0, array.Length, comparer);
        public static int UpperBound<T>(T[] array, T target) => UpperBound(array, target, 0, array.Length);
    }
}


Measured memory_bytes: 14944