To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:
 - 1 yen (the currency of Japan)
 - 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...
 - 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...
At least how many operations are required to withdraw exactly N yen in total?
It is not allowed to re-deposit the money you withdrew.

-----Constraints-----
 - 1 \leq N \leq 100000
 - N is an integer.

-----Input-----
Input is given from Standard Input in the following format:
N

-----Output-----
If at least x operations are required to withdraw exactly N yen in total, print x.

-----Sample Input-----
127

-----Sample Output-----
4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Difficulty: EASY

Solutions:
def main():
	import numpy as np
	N = int(input())
	can = [1]
	limit = 100
	for i in range(1, limit):
		six = 6 ** i
		if six <= N:
			can.append(six)
		nine = 9 ** i
		if nine <= N:
			can.append(nine)
		if six > N and nine > N:
			break
	INF = 10 ** 9
	dp = np.full(N + 1, INF, dtype=np.int32)
	dp[0] = 0
	for c in can:
		for _ in range(9):
			dp[c:] = np.minimum(dp[c:], dp[:-c] + 1)
	print(dp[-1])

def __starting_point():
	main()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 8)
N = int(input())
ss = set()
n = 6
while n <= 10 ** 5:
	ss.add(n)
	n *= 6
n = 9
while n <= 10 ** 5:
	ss.add(n)
	n *= 9
dp = [None] * (N + 1)

def rec(n):
	if dp[n] is not None:
		return dp[n]
	tmp = n
	for s in ss:
		if s > n:
			continue
		if s == n:
			tmp = 1
			break
		tmp = min(tmp, 1 + rec(n - s))
	dp[n] = tmp
	return tmp
print(rec(N))


import sys
import math
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().strip()
NI = lambda : int(input())
NMI = lambda : map(int, input().split())
NLI = lambda : list(NMI())
SI = lambda : input()

def make_grid(h, w, num):
	return [[int(num)] * w for _ in range(h)]

def get_num_draw(n, x):
	res = 0
	while n > 0:
		res += n % x
		n = n // x
	return res

def main():
	N = NI()
	ans = 10 ** 10
	for i in range(0, N + 1, 6):
		ans = min(get_num_draw(N - i, 9) + get_num_draw(i // 6, 6), ans)
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
dp = [10 ** 6 for _ in range(n + 1)]
dp[0] = 0
for i in range(n + 1):
	j6 = 1
	while i + j6 <= n:
		dp[i + j6] = min(dp[i] + 1, dp[i + j6])
		j6 *= 6
	j9 = 1
	while i + j9 <= n:
		dp[i + j9] = min(dp[i] + 1, dp[i + j9])
		j9 *= 9
print(dp[n])


n = int(input())

def solve(num, c):
	Result = 0
	while num > 0:
		Result += num % c
		num //= c
	return Result
result = []
for i in range(n + 1):
	result.append(solve(i, 9) + solve(n - i, 6))
print(min(result))


n = int(input())

def zzz(k, m):
	r = 0
	while k:
		r = r + k % m
		k = k // m
	return r
a = 99999
for i in range(n + 1):
	a = min(a, zzz(i, 6) + zzz(n - i, 9))
print(a)


N = int(input())
ans = N
for i in range(N + 1):
	j = N - i
	cnt = 0
	while i > 0:
		cnt += i % 6
		i //= 6
	while j > 0:
		cnt += j % 9
		j //= 9
	ans = min(ans, cnt)
print(ans)


N = int(input())
A = [1, 6, 36, 6 ** 3, 6 ** 4, 6 ** 5, 6 ** 6, 9, 81, 9 ** 3, 9 ** 4, 9 ** 5]
B = [0] + [N] * N
i = 0
while i <= N:
	for a in A:
		if a + i <= N:
			B[a + i] = min(B[a + i], B[i] + 1)
	i += 1
print(B[N])


N = int(input())
ans = N
for i in range(N + 1):
	c = 0
	t = i
	while t > 0:
		c += t % 6
		t //= 6
	t = N - i
	while t > 0:
		c += t % 9
		t //= 9
	ans = min(ans, c)
print(ans)


def sinsuuton(X, n):
	if int(X / n):
		return sinsuuton(int(X / n), n) + str(X % n)
	return str(X % n)

def sinsuutoo10(X, n):
	return sum([X[-i - 1] * n ** i for i in range(len(X))])
n = int(input())
ans = float('inf')
for i in range(n + 1):
	t = i
	now = 0
	t6 = sinsuuton(t, 6)
	now += sum([int(i) for i in t6])
	t = n - i
	t9 = sinsuuton(t, 9)
	now += sum([int(i) for i in t9])
	ans = min(ans, now)
print(ans)


n = int(input())
a_6 = [int(6 ** i) for i in range(1, 10) if 6 ** i <= 10 ** 5]
a_9 = [int(9 ** i) for i in range(1, 10) if 9 ** i <= 10 ** 5]
a = [1]
for i in a_6:
	a.append(i)
for i in a_9:
	a.append(i)
a.sort(reverse=True)
dp = [i for i in range(n + 1)]
dp[0] = 0
for i in range(n + 1):
	for j in a:
		if i + j <= n:
			dp[i + j] = min(dp[i + j], dp[i] + 1)
print(dp[n])


N = int(input())
l = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
dp = [N] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	for j in l:
		if i - j >= 0:
			dp[i] = min(dp[i], dp[i - j] + 1)
print(dp[N])


N = int(input())
t = [0] + [N] * N
for i in range(1, N + 1):
	(x, a, b) = (t[i - 1], 6, 9)
	while a <= i:
		x = min(x, t[i - a], t[i - b] if b <= i else N)
		a *= 6
		b *= 9
	t[i] = x + 1
print(t[N])


n = int(input())
l = [1]
for i in [6, 9]:
	x = i
	while x <= n:
		l.append(x)
		x *= i
dp = [10 ** 9] * (n + 1)
dp[0] = 0
for x in l:
	for i in range(n):
		if i + x <= n:
			dp[i + x] = min(dp[i + x], dp[i] + 1)
print(dp[n])


n = int(input())
res = n
for i in range(n + 1):
	(cc, t) = (0, i)
	while t > 0:
		cc += t % 6
		t //= 6
	t = n - i
	while t > 0:
		cc += t % 9
		t //= 9
	if res > cc:
		res = cc
print(res)


N = int(input())
dp = [N for i in range(N + 1)]
pow6 = [6 ** i for i in range(1, 8)]
pow9 = [9 ** i for i in range(1, 7)]
dp[0] = 0
for i in range(N + 1):
	dp[i] = min(dp[i], dp[i - 1] + 1)
	for j in pow6:
		if i - j >= 0:
			dp[i] = min(dp[i], dp[i - j] + 1)
	for j in pow9:
		if i - j >= 0:
			dp[i] = min(dp[i], dp[i - j] + 1)
print(dp[-1])


n = int(input())
A = [0, 1] + [6 ** i for i in range(1, 7)] + [9 ** i for i in range(1, 6)]
dp = [10 ** 6] * (10 ** 6 + 1)
dp[0] = 0
for i in range(1, n + 1):
	dp[i] = min((dp[i - a] + 1 for a in A if i >= a))
print(dp[n])


N = int(input())
dp = [float('INF')] * (N + 2)
dp[0] = 0
(a, b, c) = (1, 1, 0)
for i in range(1, N + 1):
	if i == 6 * a:
		a *= 6
	if i == 9 * b:
		b *= 9
	dp[i] = min(dp[i], dp[i - 1], dp[i - a], dp[i - b]) + 1
print(dp[N])


n = int(input())
ans = n
tmp = 0
for i in range(n + 1):
	t = i
	while t > 0:
		tmp += t % 9
		t //= 9
	t = n - i
	while t > 0:
		tmp += t % 6
		t //= 6
	if ans > tmp:
		ans = tmp
	tmp = 0
print(ans)


N = int(input())
dp = [i for i in range(N + 1)]
dp[0] = 0
for i in range(N + 1):
	for amount in (6, 9):
		a = amount
		while i + a < len(dp):
			dp[i + a] = min(dp[i + a], dp[i] + 1)
			a *= amount
print(dp[N])


N = int(input())
patterns = []
patterns.append(1)
f = 6
a = 6
while a <= N:
	patterns.append(a)
	a *= f
f = 9
a = 9
while a <= N:
	patterns.append(a)
	a *= f
dp = [float('inf')] * (N + 1)
dp[0] = 0
for n in range(1, N + 1):
	for p in patterns:
		if n - p < 0:
			continue
		dp[n] = min(dp[n - p] + 1, dp[n])
print(dp[-1])


n = int(input())
ans = n
for i in range(n + 1):
	cc = 0
	t = i
	while t > 0:
		cc += t % 6
		t //= 6
	t = n - i
	while t > 0:
		cc += t % 9
		t //= 9
	ans = min(ans, cc)
print(ans)


n = int(input())

def f(x):
	ans = 0
	while x > 0:
		ans += x % 6
		x = x // 6
	return ans

def g(x):
	ans = 0
	while x > 0:
		ans += x % 9
		x = x // 9
	return ans
ans = n
for i in range(n + 1):
	ans = min(ans, f(i) + g(n - i))
print(ans)


n = int(input())

def f(n, c):
	res = 0
	while n > 0:
		res += n % c
		n //= c
	return res
l = []
for i in range(n + 1):
	l.append(f(i, 9) + f(n - i, 6))
print(min(l))


N = int(input())
cnt = [0 for _ in range(N + 1)]
for i in range(1, N + 1):
	cnt[i] = cnt[i - 1] + 1
	a = 6
	while i - a >= 0:
		cnt[i] = min(cnt[i], cnt[i - a] + 1)
		a *= 6
	a = 9
	while i - a >= 0:
		cnt[i] = min(cnt[i], cnt[i - a] + 1)
		a *= 9
print(cnt[N])


import sys
if sys.platform == 'ios':
	sys.stdin = open('input_file.txt')
N = int(input())
ans = N
for i in range(N + 1):
	cnt = 0
	t = i
	while t > 0:
		cnt += t % 6
		t //= 6
	j = N - i
	while j > 0:
		cnt += j % 9
		j //= 9
	ans = min(ans, cnt)
print(ans)


n = int(input())
dp = [0] * (n + 1)
for i in range(n + 1):
	t = i
	count = 0
	while t > 0:
		count += t % 6
		t //= 6
	t = n - i
	while t > 0:
		count += t % 9
		t //= 9
	dp[i] = count
print(min(dp))


def main():
	N = int(input())
	INF = float('inf')
	dp = [INF] * (N + 1)
	dp[0] = 0
	for n in range(1, N + 1):
		dp[n] = 1 + dp[n - 1]
		for k in range(1, 8):
			p = 6 ** k
			if n - p >= 0:
				dp[n] = min(dp[n], dp[n - p] + 1)
			p = 9 ** k
			if n - p >= 0:
				dp[n] = min(dp[n], dp[n - p] + 1)
	ans = dp[N]
	print(ans)

def __starting_point():
	main()
__starting_point()


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return [float(x) for x in input().split()]

def LSS():
	return input().split()

def resolve():
	N = I()
	l = [1]
	a = 6
	b = 9
	while a <= N or b <= N:
		if a < b:
			l.append(a)
			a *= 6
		else:
			l.append(b)
			b *= 9
	dp = [INF] * (N + 1)
	dp[0] = 0
	for i in range(1, N + 1):
		for j in l:
			if i - j >= 0:
				dp[i] = min(dp[i - j] + 1, dp[i])
	print(dp[-1])

def __starting_point():
	resolve()
__starting_point()


import math
n = int(input())
dp = [math.inf] * (n + 1)
for i in range(n + 1):
	dp[i] = min(dp[i], i)
	j = 6
	while i + j <= n:
		dp[i + j] = min(dp[i + j], dp[i] + 1)
		j *= 6
	j = 9
	while i + j <= n:
		dp[i + j] = min(dp[i + j], dp[i] + 1)
		j *= 9
print(dp[n])


from collections import deque
N = int(input())
units = set()
i = 0
while 6 ** i <= N:
	units.add(6 ** i)
	i += 1
i = 0
while 9 ** i <= N:
	units.add(9 ** i)
	i += 1
units = sorted(units)
mins = list(range(N + 1))
for i in range(1, len(units)):
	unit = units[i]
	for j in range(N):
		if j + unit < len(mins) and mins[j] + 1 < mins[j + unit]:
			mins[j + unit] = mins[j] + 1
print(mins[N])


def f(N, x):
	ret = 0
	while N >= x:
		ret += N % x
		N = N // x
	return ret + N
N = int(input())
ans = []
for i in range(N + 1):
	ans.append(f(i, 6) + f(N - i, 9))
print(min(ans))


N = int(input())

def f(x, base):
	ret = 0
	while x:
		(x, m) = divmod(x, base)
		ret += m
	return ret
print(min([f(i, 6) + f(N - i, 9) for i in range(N + 1)]))


N = int(input())
dp = [i for i in range(N + 1)]
for i in range(N + 1):
	index = 1
	while 6 ** index <= i:
		dp[i] = min(dp[i], dp[i - 6 ** index] + 1)
		index += 1
	index = 1
	while 9 ** index <= i:
		dp[i] = min(dp[i], dp[i - 9 ** index] + 1)
		index += 1
print(dp[N])


N = int(input())
ans = N
for i in range(N + 1):
	t = i
	cc = 0
	while t > 0:
		cc += t % 6
		t //= 6
	t = N - i
	while t > 0:
		cc += t % 9
		t //= 9
	if ans > cc:
		ans = cc
print(ans)


N = int(input())
ans = 10 ** 5
for i in range(N + 1):
	j = N - i
	count = 0
	while i > 0:
		count += i % 6
		i //= 6
	while j > 0:
		count += j % 9
		j //= 9
	ans = min(ans, count)
print(ans)


n = int(input())
dp = [0] * (n + 10)
for i in range(n + 1):
	dp[i + 1] = dp[i] + 1
for i in range(1, n + 1):
	k = 6
	while k <= n:
		if i - k >= 0:
			dp[i] = min(dp[i], dp[i - k] + 1)
		k *= 6
	k = 9
	while k <= n:
		if i - k >= 0:
			dp[i] = min(dp[i], dp[i - k] + 1)
		k *= 9
print(dp[n])


N = int(input())
INF = 10 ** 9
dp = [0] + [INF for _ in range(N)]
for i in range(1, N + 1):
	dp[i] = dp[i - 1] + 1
	for j in [6, 9]:
		k = j
		while k <= i:
			dp[i] = min(dp[i], dp[i - k] + 1)
			k *= j
print(dp[N])


n = int(input())
a = n
for i in range(n + 1):
	(ni, si) = (i, n - i)
	m = 0
	while ni != 0:
		m += ni % 9
		ni //= 9
	while si != 0:
		m += si % 6
		si //= 6
	a = min(a, m)
print(a)


n = int(input())
m = n // 3
r = n % 3
a = [0]
for i in range(1, m + 1):
	t = a[i - 1] + 3
	j = 2
	while i >= j:
		t = min(t, a[i - j] + 1)
		j *= 6
	k = 3
	while i >= k:
		t = min(t, a[i - k] + 1)
		k *= 9
	a.append(t)
print(a[m] + r)


n = int(input())
dp = [2000000000] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	k = 1
	while k <= i:
		dp[i] = min(dp[i], dp[i - k] + 1)
		k *= 6
	k = 9
	while k <= i:
		dp[i] = min(dp[i], dp[i - k] + 1)
		k *= 9
print(dp[n])


N = int(input())

def calc(x, base):
	r = 0
	while x:
		(x, m) = divmod(x, base)
		r += m
	return r
result = min((calc(i, 6) + calc(N - i, 9) for i in range(N + 1)))
print(result)


inf = 10 ** 7
n = int(input())
ad = [6 ** x for x in range(10)]
ad2 = [9 ** x for x in range(1, 10)]
ad.extend(ad2)
a = [inf] * (n + 1)
a[0] = 0
for i in range(n + 1):
	for x in ad:
		if x + i <= n:
			a[x + i] = min(a[x + i], a[i] + 1)
print(a[n])


N = int(input())
dp = [float('INF')] * (N + 1)
dp[0] = 0
(six, nine) = (1, 1)
for i in range(1, N + 1):
	if i == six * 6:
		six *= 6
	if i == nine * 9:
		nine *= 9
	dp[i] = min(dp[i - 1], dp[i - six], dp[i - nine]) + 1
print(dp[N])


def times(money, mod):
	ans = 0
	while money != 0:
		ans += money % mod
		money = money // mod
	return ans
c = []
n = int(input())
for i in range(n + 1):
	c.append(times(i, 6) + times(n - i, 9))
print(min(c))


N = int(input())
ans = N
for i in range(N + 1):
	n = i
	m = N - i
	res = 0
	nine = 1
	while nine * 9 <= n:
		nine *= 9
	while nine != 1:
		res += n // nine
		n %= nine
		nine /= 9
	res += n
	six = 1
	while six * 6 <= m:
		six *= 6
	while six != 1:
		res += m // six
		m %= six
		six /= 6
	res += m
	ans = min(ans, int(res))
print(ans)


N = int(input())
a = N
for n in range(N + 1):
	c = 0
	t = n
	while t > 0:
		c += t % 6
		t //= 6
	t = N - n
	while t > 0:
		c += t % 9
		t //= 9
	a = min(a, c)
print(a)


N = int(input())

def cou(n, x):
	i = 0
	while n > 0:
		i += n % x
		n //= x
	return i
ans = N
for j in range(N + 1):
	ans = min(ans, cou(j, 6) + cou(N - j, 9))
print(ans)


N = int(input())
A = [1, 6, 6 ** 2, 6 ** 3, 6 ** 4, 6 ** 5, 6 ** 6, 9, 9 ** 2, 9 ** 3, 9 ** 4, 9 ** 5]
dp = [1000000] * 300000
dp[0] = 0
for i in range(N):
	for a in A:
		d = dp[i] + 1
		dp[i + a] = min(dp[i + a], d)
print(dp[N])


import numpy as np
n = int(input())
dp = np.full(n + 1, n + 1, dtype=int)
dp[0] = 0
loop = 0
while dp[n] > n:
	tmp1 = np.full(n + 1, n + 1, dtype=int)
	tmp6 = np.full(n + 1, n + 1, dtype=int)
	tmp9 = np.full(n + 1, n + 1, dtype=int)
	tmp1[1:] = dp[:-1] + 1
	j = 1
	while 6 ** j <= n:
		tmp6_pre = tmp6
		tmp6[6 ** j:] = dp[:-6 ** j] + 1
		tmp6 = np.minimum(tmp6_pre, tmp6)
		j += 1
	k = 1
	while 9 ** k <= n:
		tmp9_pre = tmp9
		tmp9[9 ** k:] = dp[:-9 ** k] + 1
		tmp9 = np.minimum(tmp9_pre, tmp9)
		k += 1
	tmp1 = np.minimum(tmp1, tmp6)
	tmp1 = np.minimum(tmp1, tmp9)
	dp = np.minimum(dp, tmp1)
	loop += 1
print(dp[n])


n = int(input())
ans = n
for i in range(n + 1):
	cnt = 0
	t = i
	while t > 0:
		cnt += t % 6
		t //= 6
	j = n - i
	while j > 0:
		cnt += j % 9
		j //= 9
	ans = min(ans, cnt)
print(ans)


s = lambda a, b: a if a < b else s(a // b, b) + a % b
n = int(input())
print(min((s(i, 6) + s(n - i, 9) for i in range(n + 1))))


def f(yen, coin, cnt=0):
	while yen > 0:
		cnt += yen % coin
		yen //= coin
	return cnt
ans = n = int(input())
for yen6 in range(0, n + 1):
	ans = min(ans, f(n - yen6, 9, f(yen6, 6)))
print(ans)


n = int(input())
a = n
for i in range(n + 1):
	(c, j) = (0, n - i)
	while i:
		c += i % 6
		i //= 6
	while j:
		c += j % 9
		j //= 9
	a = min(a, c)
print(a)


n = int(input())
dp = [float('inf')] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	for j in [6, 9]:
		cnt = 1
		while i + cnt <= n:
			if dp[i + cnt] > dp[i] + 1:
				dp[i + cnt] = dp[i] + 1
			cnt *= j
print(dp[-1])


import sys
readline = sys.stdin.readline
N = int(readline())
INF = 10 ** 10
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(10, 0, -1):
	for j in range(N + 1):
		if dp[j] != INF:
			if j + 6 ** i <= N:
				dp[j + 6 ** i] = min(dp[j + 6 ** i], dp[j] + 1)
			if j + 9 ** i <= N:
				dp[j + 9 ** i] = min(dp[j + 9 ** i], dp[j] + 1)
ans = INF
for i in range(len(dp) - 1, -1, -1):
	money = dp[i] + N - i
	if ans > money:
		ans = money
print(ans)


N = int(input())
ans = N
for i in range(N + 1):
	cnt = 0
	s = i
	while s > 0:
		cnt += s % 6
		s //= 6
	n = N - i
	while n > 0:
		cnt += n % 9
		n //= 9
	ans = min(ans, cnt)
print(ans)


def f(num, base):
	ret = 1
	while num >= ret:
		ret *= base
	return ret // base
a = {int(input())}
cnt = 0
while all(a):
	a = {x - f(x, 6) for x in a} | {x - f(x, 9) for x in a}
	cnt += 1
print(cnt)


N = int(input())
ans = 10 ** 5
for i in range(N + 1):
	cc = 0
	(t, s) = (i, N - i)
	while t > 0:
		cc += t % 6
		t //= 6
	while s > 0:
		cc += s % 9
		s //= 9
	ans = min(ans, cc)
print(ans)


n = int(input())
ans = float('INF')
for i in range(n + 1):
	d = 0
	temp = i
	while temp > 0:
		d += temp % 6
		temp //= 6
	temp = n - i
	while temp > 0:
		d += temp % 9
		temp //= 9
	ans = min(d, ans)
print(ans)


n = int(input())

def calc(c, p):
	res = 0
	while c:
		res += c % p
		c = c // p
	return res
ans = n
for i in range(n + 1):
	cand = calc(i, 6) + calc(n - i, 9)
	ans = min(ans, cand)
print(ans)


N = int(input())
ans = 1000000000.0
for i in range(N + 1):
	cnt = 0
	tmp = i
	while tmp > 0:
		cnt += tmp % 6
		tmp //= 6
	tmp = N - i
	while tmp > 0:
		cnt += tmp % 9
		tmp //= 9
	ans = min(ans, cnt)
print(ans)


n = int(input())
res = n
for i in range(n + 1):
	count = 0
	j = n - i
	while i > 0:
		count += i % 6
		i //= 6
	while j > 0:
		count += j % 9
		j //= 9
	if res > count:
		res = count
print(int(res))


n = int(input())
(index6, index9, num6, num9) = (0, 0, 0, 0)
INF = float('inf')
a = [INF for _ in range(n + 1)]
a[n] = 0
for i in range(n, -1, -1):
	if a[i] == INF:
		continue
	j = 1
	while i - 9 ** j >= 0:
		a[i - 9 ** j] = min(a[i] + 1, a[i - 9 ** j])
		j += 1
	j = 1
	while i - 6 ** j >= 0:
		a[i - 6 ** j] = min(a[i] + 1, a[i - 6 ** j])
		j += 1
	if i - 1 >= 0:
		a[i - 1] = min(a[i] + 1, a[i - 1])
print(a[0])


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

def MI():
	return map(int, input().split())

def MF():
	return map(float, input().split())

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

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	N = I()
	L = [1]
	temp = 1
	while temp <= N:
		temp *= 6
		L.append(temp)
	temp = 1
	while temp <= N:
		temp *= 9
		L.append(temp)
	L.sort()
	D = [i for i in range(N + 1)]
	for i in L[1:]:
		for j in range(i, N + 1):
			D[j] = min(D[j], D[j - i] + 1)
	print(D[N])

def __starting_point():
	main()
__starting_point()


n = int(input())
print(min((sum((i // 9 ** j % 9 + (n - i) // 6 ** j % 6 for j in range(7))) for i in range(n + 1))))


import sys
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
from itertools import product, accumulate, combinations, product
INF = 1 << 50
EPS = 1e-08
mod = 10 ** 9 + 7

def run():
	N = int(input())
	nines = [9 ** i for i in range(1, 10) if 9 ** i <= N]
	sixes = [6 ** i for i in range(1, 10) if 6 ** i <= N][::-1]
	L = len(nines)
	min_ans = INF
	for K in product(list(range(9)), repeat=L):
		ans = 0
		s = 0
		for (k, x) in zip(K, nines):
			if k:
				ans += k
				s += x * k
		if s > N:
			continue
		resid = N - s
		for six in sixes:
			tmp = resid // six
			if tmp:
				ans += tmp
				resid -= six * tmp
		ans += resid
		min_ans = min(min_ans, ans)
	print(min_ans)

def __starting_point():
	run()
__starting_point()


N = int(input())
dp = [i for i in range(N + 1)]
n = 1
while n <= N:
	n *= 6
	for j in range(N - n + 1):
		dp[j + n] = min(dp[j + n], dp[j] + 1)
n = 1
while n <= N:
	n *= 9
	for j in range(N - n + 1):
		dp[j + n] = min(dp[j + n], dp[j] + 1)
print(dp[N])


N = int(input())
dp = [float('inf')] * (N + 1)
dp[0] = 0
operates = sorted([1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049])
for i in range(N):
	for ope in operates:
		if i + ope > N:
			break
		dp[i + ope] = min(dp[i + ope], dp[i] + 1)
print(dp[N])


ref = [6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049]
N = int(input())
dp = list(range(N + 1))
for x in ref:
	for i in range(x, N + 1):
		y = dp[i - x] + 1
		if y < dp[i]:
			dp[i] = y
print(dp[N])


import math
N = int(input())
INF = 10 ** 10
dp = [0] * (N + 1)
for i in range(N + 1):
	dp[i] = INF
dp[0] = 0
W = [1, 6, 9]
for v in range(1, N + 1):
	for w in range(3):
		for x in range(int(math.log(N + 1, 6)) + 1):
			n = W[w] ** x
			if v - n < 0:
				continue
			dp[v] = min(dp[v], dp[v - n] + 1)
print(dp[N])


n = int(input())
l = sorted([6 ** i for i in range(1, 7)] + [9 ** i for i in range(6)])
dp = [float('inf')] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in range(12):
		if i - l[j] >= 0:
			dp[i] = min(dp[i], dp[i - l[j]] + 1)
print(dp[-1])


def rec(n, x):
	(d, m) = divmod(n, x)
	if d:
		return m + rec(d, x)
	else:
		return m
ans = n = int(input())
for i in range(n + 1):
	ans = min(ans, rec(i, 6) + rec(n - i, 9))
print(ans)


def count(val1, val2):
	num = 1
	while True:
		if val1 < val2 ** num:
			break
		num += 1
	cnt = 0
	digit = num
	while True:
		if val1 >= val2 ** digit:
			val1 -= val2 ** digit
			cnt += 1
		else:
			digit -= 1
		if digit == 0:
			break
	return (cnt, val1)
N = int(input())
ans = N
for i in range(0, N + 1):
	n = i
	m = N - i
	res = 0
	(cnt, nn) = count(n, 9)
	n = nn
	res += cnt
	(cnt, mm) = count(m, 6)
	m = mm
	res += cnt
	res += n + m
	ans = min(ans, res)
print(ans)


N = int(input())
x = N
for i in range(N + 1):
	count = 0
	t = i
	while t > 0:
		count += int(t % 9)
		t = int(t / 9)
	t = N - i
	while t > 0:
		count += int(t % 6)
		t = int(t / 6)
	if x > count:
		x = count
print(x)


n = int(input())
m = n // 6 + n % 6
for i in range(n + 1):
	c = 0
	t = i
	while t > 0:
		c += t % 6
		t //= 6
	t = n - i
	while t > 0:
		c += t % 9
		t //= 9
	m = min(m, c)
print(m)


n = int(input())
ans = n
for n1 in range(n + 1):
	n2 = n - n1
	cnt1 = 0
	while n1 > 0:
		cnt1 += n1 % 6
		n1 = n1 // 6
	cnt2 = 0
	while n2 > 0:
		cnt2 += n2 % 9
		n2 = n2 // 9
	ans = min(ans, cnt1 + cnt2)
print(ans)


n = int(input())
tab = [10 ** 8 for i in range(n + 1)]
tab[0] = 0
for i in range(n):
	now = 1
	while i + now <= n:
		tab[i + now] = min(tab[i + now], tab[i] + 1)
		now *= 6
	now = 9
	while i + now <= n:
		tab[i + now] = min(tab[i + now], tab[i] + 1)
		now *= 9
print(tab[-1])


def fn(n, a):
	res = 0
	while n > 0:
		res += n % a
		n //= a
	return res

def solve(N):
	ans = 10 ** 5
	for i in range(N + 1):
		tmp = fn(i, 6) + fn(N - i, 9)
		ans = min(ans, tmp)
	return ans

def __starting_point():
	N = int(input())
	print(solve(N))
__starting_point()


def it(n, p):
	while n > 0:
		yield (n % p)
		n //= p
cost = lambda *a: sum(it(*a))
n = int(input())
print(min((cost(x, 6) + cost(n - x, 9) for x in range(n + 1))))


def s(n, r):
	if n == 0:
		return 0
	return n % r + s(n // r, r)
n = int(input())
ans = 100000
for i in range(n + 1):
	ans = min(ans, s(i, 6) + s(n - i, 9))
print(ans)


n = int(input())
dp = list((i for i in range(n + 1)))

def f(yen, p):
	c = p
	while yen >= c:
		dp[yen] = min(dp[yen], dp[yen - c] + 1)
		c *= p
for yen in range(6, n + 1):
	f(yen, 6)
	f(yen, 9)
print(dp[n])


N = int(input())
ans = 100000
for A in range(N + 1):
	num = 0
	B = N - A
	while A != 0:
		num += A % 6
		A = A // 6
	while B != 0:
		num += B % 9
		B = B // 9
	ans = min(ans, num)
print(ans)


N = int(input())
ans = float('Inf')
for i in range(N + 1):
	k = N - i
	k2 = i
	six = 0
	nine = 0
	while k2 > 0:
		six += k2 % 6
		k2 //= 6
	while k > 0:
		nine += k % 9
		k //= 9
	ans = min(ans, six + nine)
print(ans)


def calc(n, num):
	ret = 0
	while n > 0:
		ret += n % num
		n //= num
	return ret
N = int(input())
ans = N
for i in range(N + 1):
	cnt = 0
	cnt += calc(i, 6)
	cnt += calc(N - i, 9)
	ans = min(ans, cnt)
print(ans)


N = int(input())
t = {0: 0}
for i in range(1, N + 1):
	(x, j) = (N, 0)
	while 6 ** j <= i:
		x = min(x, t[i - 6 ** j], t.get(i - 9 ** j, N))
		j += 1
	t[i] = x + 1
print(t[N])


N = int(input())
min_ = N
for i in range(N + 1):
	x = 0
	n = i
	while n > 0:
		x += n % 6
		n //= 6
	n = N - i
	while n > 0:
		x += n % 9
		n //= 9
	min_ = min(min_, x)
print(min_)


N = int(input())
lst = [1, 6, 9, 6 ** 2, 9 ** 2, 6 ** 3, 9 ** 3, 6 ** 4, 9 ** 4, 6 ** 5, 6 ** 6, 9 ** 5]
dp = [N + 1] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	for j in lst:
		if j <= i:
			dp[i] = min(dp[i], dp[i - j] + 1)
print(dp[-1])


n = int(input())
ans = n
for i in range(n + 1):
	c = 0
	a = n - i
	while a > 0:
		c += a % 6
		a = a // 6
	a = i
	while a > 0:
		c += a % 9
		a = a // 9
	if c < ans:
		ans = c
print(ans)


def dfs(i, n):
	if n < 6:
		return i + n
	k = 1
	while k * 6 <= n:
		k *= 6
	l = 1
	while l * 9 <= n:
		l *= 9
	return min(dfs(i + 1, n - k), dfs(i + 1, n - l))
N = int(input())
print(dfs(0, N))


N = int(input())
dp = [N] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	dp[i] = dp[i - 1] + 1
	for k in range(1, 10):
		if i < 6 ** k:
			break
		else:
			dp[i] = min(dp[i], dp[i - 6 ** k] + 1)
	for k in range(1, 10):
		if i < 9 ** k:
			break
		else:
			dp[i] = min(dp[i], dp[i - 9 ** k] + 1)
print(dp[N])


n = int(input())
dp = [1000000 for _ in range(n + 1)]
dp[0] = 0
for i in range(0, n):
	if i + 1 <= n:
		dp[i + 1] = min(dp[i + 1], dp[i] + 1)
	for j in range(1, 7):
		a = 6 ** j
		if i + a <= n:
			dp[i + a] = min(dp[i + a], dp[i] + 1)
	for k in range(1, 6):
		a = 9 ** k
		if i + a <= n:
			dp[i + a] = min(dp[i + a], dp[i] + 1)
print(dp[n])


def main():
	import sys

	def input():
		return sys.stdin.readline().rstrip()
	n = int(input())
	inf = 1 << 18
	dp = [inf] * (n + 1)
	dp[0] = 0
	for i in range(n):
		dp[i + 1] = min(dp[i + 1], dp[i] + 1)
		k = 1
		while i + 6 ** k <= n:
			dp[i + 6 ** k] = min(dp[i + 6 ** k], dp[i] + 1)
			k += 1
		k = 1
		while i + 9 ** k <= n:
			dp[i + 9 ** k] = min(dp[i + 9 ** k], dp[i] + 1)
			k += 1
	print(dp[n])

def __starting_point():
	main()
__starting_point()


N = int(input())
ans = N
for i in range(N + 1):
	x = 0
	y = i
	while y:
		x += y % 6
		y //= 6
	y = N - i
	while y:
		x += y % 9
		y //= 9
	ans = min(x, ans)
print(ans)


N = int(input())
res = N
for i in range(N + 1):
	cc = 0
	t = i
	while t > 0:
		cc += t % 6
		t = t // 6
	t = N - i
	while t > 0:
		cc += t % 9
		t = t // 9
	if res > cc:
		res = cc
import math
print(math.floor(res))


n = int(input())

def k(a, b):
	ans = 0
	while a != 0:
		ans += a % b
		a //= b
	return ans
res = 10 ** 10
for i in range(n + 1):
	res = min(res, k(i, 9) + k(n - i, 6))
print(res)


N = int(input())
dp = [10 ** 100] * (N + 10 ** 6)
dp[0] = 0
delta = [1, 6, 6 ** 2, 6 ** 3, 6 ** 4, 6 ** 5, 6 ** 6, 9, 9 ** 2, 9 ** 3, 9 ** 4, 9 ** 5]
for i in range(0, N):
	for d in delta:
		dp[i + d] = min(dp[i + d], dp[i] + 1)
print(dp[N])


n = int(input())

def Count(x, p):
	res = 0
	while x:
		res += x % p
		x = x // p
	return res
ans = 10 ** 6 + 1
for i in range(0, n + 1):
	ans = min(ans, Count(i, 6) + Count(n - i, 9))
print(ans)


n = int(input())

def func(n, i):
	ans = 0
	while n > 0:
		ans += n % i
		n //= i
	return ans
b = n
for i in range(n + 1):
	tmp = func(i, 6) + func(n - i, 9)
	b = min(tmp, b)
print(b)


def s(n, p):
	while n > 0:
		yield (n % p)
		n //= p
c = lambda *a: sum(s(*a))
n = int(input())
print(min((c(i, 6) + c(n - i, 9) for i in range(n + 1))))


n = int(input())
a = [0] * (n + 1)
six = 0
nine = 0
for i in range(1, n + 1):
	if i >= 6 ** (six + 1):
		six += 1
	if i >= 9 ** (nine + 1):
		nine += 1
	a[i] = min(a[i - 1], a[i - 6 ** six], a[i - 9 ** nine]) + 1
print(a[n])


N = int(input())
ans = N
for i in range(N + 1):
	t = i
	cnt = 0
	while t > 0:
		cnt += t % 6
		t //= 6
	k = N - i
	while k > 0:
		cnt += k % 9
		k //= 9
	ans = min(ans, cnt)
print(ans)


def f(a, x):
	l = []
	while x != 0:
		l.append(x % a)
		x //= a
	return sum(l)

def main():
	N = int(input())
	MIN = 100000
	for k in range(N + 1):
		MIN = min(MIN, f(9, k) + f(6, N - k))
	return MIN

def __starting_point():
	print(main())
__starting_point()


def base(x, p):
	if int(x / p) >= 1:
		return base(int(x / p), p) + str(x % p)
	return str(x % p)
n = int(input())
c = 10 ** 9
for i in range(n + 1):
	c = min(c, sum(list(map(int, base(i, 6) + base(n - i, 9)))))
print(c)


n = int(input())
mi = n
for k in range(n + 1):
	six = 0
	nine = 0
	k2 = n - k
	while k > 0:
		six += k % 6
		k //= 6
	while k2 > 0:
		nine += k2 % 9
		k2 //= 9
	mi = min(mi, six + nine)
print(mi)


n = int(input())
cnt = float('inf')
for i in range(0, n + 1):
	j = n - i
	temp = 0
	while i:
		temp += i % 9
		i //= 9
	while j:
		temp += j % 6
		j //= 6
	cnt = min(temp, cnt)
print(cnt)


coin = [1, 6, 6 ** 2, 6 ** 3, 6 ** 4, 6 ** 5, 6 ** 6, 9, 9 ** 2, 9 ** 3, 9 ** 4, 9 ** 5]
n = int(input())
inf = 10 ** 9
dp = [inf] * (n + 1)
dp[0] = 0
for c in coin:
	for yen in range(c, n + 1):
		dp[yen] = min(dp[yen], dp[yen - c] + 1)
print(dp[n])


n = int(input())
res = n
for i in range(n + 1):
	cnt = 0
	t = i
	while t > 0:
		cnt += t % 6
		t //= 6
	t = n - i
	while t > 0:
		cnt += t % 9
		t //= 9
	res = min(res, cnt)
print(res)


n = int(input())
dp = [2 ** 60] * (100000 + n)
dp[0] = 0
for i in range(n + 1):
	for j in range(7):
		dp[i] = min(dp[i - 6 ** j] + 1, dp[i])
	for k in range(6):
		dp[i] = min(dp[i], dp[i - 9 ** k] + 1)
print(dp[n])


N = int(input())

def func(n, a):
	cnt = 0
	while n != 0:
		r = n % a
		n = (n - r) // a
		cnt += r
	return cnt
ans = N
for i in range(N + 1):
	tmp = func(i, 6) + func(N - i, 9)
	ans = min(ans, tmp)
print(ans)


N = int(input())
INF = 10 ** 9

def base(x, y):
	ret = 0
	while x >= y:
		ret += x % y
		x //= y
	ret += x
	return ret

def main():
	ans = INF
	for i in range(N + 1):
		v = base(i, 6) + base(N - i, 9)
		ans = min(ans, v)
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
a = n
for i in range(n + 1):
	x = i
	y = n - i
	c = 0
	while x >= 6:
		c += x % 6
		x = x // 6
	c += x
	while y >= 9:
		c += y % 9
		y = y // 9
	c += y
	a = min(a, c)
print(a)


n = int(input())
r = n

def c(c, n):
	s = 0
	while n > 0:
		s += n % c
		n //= c
	return s
for i in range(n + 1):
	r = min(c(6, i) + c(9, n - i), r)
print(r)


N = int(input())

def shin(n, q):
	s = n
	a = 0
	while s > 0:
		t = s % q
		a += t
		s = (s - t) // q
	return a
ans = N
for i in range(N + 1):
	c = shin(i, 6) + shin(N - i, 9)
	ans = min(ans, c)
print(ans)


n = int(input())
l = [1]
l += [6 ** i for i in range(1, 7)]
l += [9 ** i for i in range(1, 6)]
l.sort()
dp = [0] * (n + 1)
for i in range(1, n + 1):
	cand = []
	for j in l:
		if i >= j:
			cand.append(dp[i - j] + 1)
	dp[i] = min(cand)
print(dp[n])


def getval():
	n = int(input())
	return n

def main(n):
	dp = [0]
	arr = [1]
	a1 = 6
	a2 = 9
	for i in range(1, 100):
		if a1 ** i <= n:
			arr.append(a1 ** i)
		else:
			break
	for i in range(1, 100):
		if a2 ** i <= n:
			arr.append(a2 ** i)
		else:
			break
	arr.sort()
	for i in range(1, n + 1):
		temp = i
		for j in arr:
			if j > i:
				break
			temp = min(temp, dp[i - j] + 1)
		dp.append(temp)
	print(dp[n])

def __starting_point():
	n = getval()
	main(n)
__starting_point()


def sub_cnt(c, b):
	cnt = 0
	for i in b:
		while c >= i:
			c -= i
			cnt += 1
	return cnt
n = int(input())
b6 = [6 ** i for i in range(6, -1, -1)]
b9 = [9 ** i for i in range(5, -1, -1)]
ans = 10 ** 9
n9 = n // 9
for c9 in range(n9 + 1):
	cnt = 0
	cn = n - c9 * 9
	c6 = cn // 6
	c1 = cn % 6
	cnt += sub_cnt(c9, b9)
	cnt += sub_cnt(c6, b6)
	cnt += c1
	ans = min(ans, cnt)
print(ans)


from functools import lru_cache

def main():
	n = int(input())
	dp = [float('inf')] * (n + 1)
	dp[0] = 0
	for i in range(n):
		dp[i + 1] = min(dp[i + 1], dp[i] + 1)
		(j, k) = (6, 9)
		while i + j <= n:
			dp[i + j] = min(dp[i + j], dp[i] + 1)
			j *= 6
		while i + k <= n:
			dp[i + k] = min(dp[i + k], dp[i] + 1)
			k *= 9
	print(dp[-1])

def __starting_point():
	main()
__starting_point()


N = int(input())

def fn(n, a):
	res = 0
	while n > 0:
		res += n % a
		n = n // a
	return res
ans = 1 << 60
for i in range(0, N + 1):
	tmp = fn(i, 6) + fn(N - i, 9)
	ans = min(ans, tmp)
print(ans)


def sum_digits(n, m):
	r = 0
	while n:
		r = r + n % m
		n = n // m
	return r
N = int(input())
ans = 100000
for i in range(N + 1):
	ans = min(ans, sum_digits(i, 6) + sum_digits(N - i, 9))
print(ans)


n = int(input())
dp = [i for i in range(n + 1)]
lw = [6, 9]
for w in lw:
	k = 1
	while w ** k <= n:
		q = w ** k
		for j in range(n + 1 - q):
			dp[j + q] = min(dp[j] + 1, dp[j + q])
		k += 1
print(dp[n])


N = int(input())
A = [1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049]
L = [0]
for i in range(1, N + 1):
	M = []
	for a in A:
		if i - a >= 0:
			M.append(L[i - a] + 1)
	L.append(min(M))
print(L[-1])


n = int(input())
ans = 1000000000
for i in range(0, n + 1, 6):
	j = n - i
	a = 0
	while j:
		a += j % 9
		j //= 9
	while i:
		a += i % 6
		i //= 6
	ans = min(a, ans)
print(ans)


n = int(input())
money = [1]
for i in range(1, 7):
	money.append(6 ** i)
for i in range(1, 6):
	money.append(9 ** i)
money = sorted(money)
lst = [0] * 1000000
for i in range(n + 1):
	can = i
	for m in money:
		if i - m >= 0:
			can = min(can, lst[i - m] + 1)
		else:
			break
	lst[i] = can
print(lst[n])


def cal(n, x):
	res = 0
	while n > 0:
		res += n % x
		n //= x
	return res
N = int(input())
ans = N
for i in range(N + 1):
	ans = min(ans, cal(i, 6) + cal(N - i, 9))
print(ans)


N = int(input())
inf = 100000
dp = [inf for _ in range(N + 1)]
dp[0] = 0
for n in range(1, N + 1):
	num = 100000
	i = 0
	j = 0
	while 9 ** (i + 1) <= n:
		i += 1
	while 6 ** (j + 1) <= n:
		j += 1
	for k in range(i + 1):
		num = min(num, dp[n - 9 ** k] + 1)
	for k in range(j + 1):
		num = min(num, dp[n - 6 ** k] + 1)
	dp[n] = min(dp[n], num)
print(dp[N])


N = int(input())
dp = [N] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	power = 1
	while power <= i:
		dp[i] = min(dp[N], dp[i - power] + 1)
		power *= 6
	power = 9
	while power <= i:
		dp[i] = min(dp[i], dp[i - power] + 1)
		power *= 9
print(dp[-1])


n = int(input())
ans = [-1] * (n + 1)
ans[0] = 0
for i in range(1, n + 1):
	num = i
	data = ans[i - 1] + 1
	for j in range(8):
		index = i - 6 ** j
		if index < 0:
			break
		data = min(data, ans[index] + 1)
	for j in range(8):
		index = i - 9 ** j
		if index < 0:
			break
		data = min(data, ans[index] + 1)
	ans[i] = data
print(ans[-1])


n = int(input())
dp = [10 ** 6] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	j = 0
	while i - 6 ** j >= 0:
		dp[i] = min(dp[i - 6 ** j] + 1, dp[i])
		j += 1
	j = 0
	while i - 9 ** j >= 0:
		dp[i] = min(dp[i - 9 ** j] + 1, dp[i])
		j += 1
print(dp[-1])


def ds(a, b):
	return a if a < b else ds(a // b, b) + a % b
n = int(input())
print(min([ds(i, 6) + ds(n - i, 9) for i in range(n + 1)]))


def s(n, c):
	if n < 6:
		return c + n
	a = b = 1
	while a * 6 <= n:
		a *= 6
	while b * 9 <= n:
		b *= 9
	return min(s(n - a, c + 1), s(n - b, c + 1))
print(s(int(input()), 0))


N = int(input())
res = 100000
for i in range(N + 1):
	cc = 0
	t = i
	while t > 0:
		cc += t % 6
		t = int(t / 6)
	t = N - i
	while t > 0:
		cc += t % 9
		t = int(t / 9)
	if res > cc:
		res = cc
print(res)


import sys
N = int(sys.stdin.readline())
s_max = N // 6
n_max = N // 9
s_n = 6
s_c = 0
while s_n < N:
	s_n *= 6
	s_c += 1
n_n = 9
n_c = 0
while n_n < N:
	n_n *= 9
	n_c += 1
ans = float('inf')
for i in range(s_max + 1):
	j = (N - 6 * i) // 9
	o = N - 6 * i - 9 * j
	if o < 0:
		continue
	tmp_ans = o + i + j
	ans = min(ans, tmp_ans)
	tmp = 6 ** s_c
	while 0 < tmp:
		r = i // tmp
		i -= r * tmp
		tmp_ans += r - r * tmp
		tmp //= 6
	tmp = 9 ** n_c
	while 0 < tmp:
		r = j // tmp
		j -= r * tmp
		tmp_ans += r - r * tmp
		tmp //= 9
	ans = min(ans, tmp_ans)
print(ans)


n = int(input())
dp = [0]
for i in range(1, n + 1):
	c = []
	c.append(dp[i - 1] + 1)
	p6 = 1
	while i >= 6 ** p6:
		c.append(dp[i - 6 ** p6] + 1)
		p6 += 1
	p9 = 1
	while i >= 9 ** p9:
		c.append(dp[i - 9 ** p9] + 1)
		p9 += 1
	dp.append(min(c))
print(dp[n])


def cnt(n, p):
	tmp = 0
	m = n
	while m > 0:
		tmp += m % p
		m //= p
	return tmp
ans = 10 ** 15
N = int(input())
for i in range(N + 1):
	ans = min(ans, cnt(i, 6) + cnt(N - i, 9))
print(ans)


n = int(input())
s = [1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049]
a = [0] * (n + 1)
for k in range(1, n + 1):
	m = n
	for x in s:
		if x <= k:
			m = min(m, a[k - x] + 1)
	a[k] = m
print(a[n])


N = int(input())

def base_n(x, n):
	results = 0
	while x > 0:
		results += x % n
		x //= n
	return results
print(min([base_n(i, 6) + base_n(N - i, 9) for i in range(N + 1)]))


N = int(input())
Cash = sorted([1] + [6 ** T for T in range(1, 7)] + [9 ** T for T in range(1, 6)])
DPList = [0] * (N + 1)
for TN in range(1, N + 1):
	DPList[TN] = min((DPList[TN - TC] + 1 if TN >= TC else 1000000 for TC in Cash))
print(DPList[N])


n = int(input())
dp = [float('inf')] * (n + 1)
for i in range(n + 1):
	dp[i] = min(dp[i], i)
	j = 6
	while i + j <= n:
		dp[i + j] = min(dp[i + j], dp[i] + 1)
		j *= 6
	k = 9
	while i + k <= n:
		dp[i + k] = min(dp[i + k], dp[i] + 1)
		k *= 9
print(dp[n])


N = int(input())
res = 10 ** 10
for i in range(N + 1):
	count = 0
	six = i
	while six > 0:
		count += six % 6
		six = six // 6
	nine = N - i
	while nine > 0:
		count += nine % 9
		nine = nine // 9
	res = min(res, count)
print(res)


n = int(input())
a = 100000
for i in range(n + 1):
	j = n - i
	count = 0
	while i > 0:
		count += i % 6
		i //= 6
	while j > 0:
		count += j % 9
		j //= 9
	a = min(a, count)
print(a)


N = int(input())
m = N
for i in range(N + 1):
	t = N - i
	count = 0
	while i > 0:
		count += i % 6
		i = i // 6
	while t > 0:
		count += t % 9
		t = t // 9
	m = min(m, count)
print(m)


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	N = int(readline())
	amt = [1]
	a = 6
	while a <= N:
		amt.append(a)
		a *= 6
	a = 9
	while a <= N:
		amt.append(a)
		a *= 9
	dp = [INF] * (N + 1)
	dp[0] = 0
	for i in range(1, N + 1):
		for a in amt:
			if i >= a and dp[i] > dp[i - a]:
				dp[i] = dp[i - a] + 1
	print(dp[N])
	return

def __starting_point():
	main()
__starting_point()


n = int(input())
A = []
for s in range(n + 1):
	count = 0
	t = n - s
	while s > 0:
		count += s % 6
		s //= 6
	while t > 0:
		count += t % 9
		t //= 9
	A.append(count)
print(min(A))


from collections import deque
n = int(input())
cnt = 0
d = {n: 0}
q = deque([n])
while q[0] > -5:
	t = q.popleft()
	cnt = d[t]
	a = 1
	b = 1
	while 6 ** a <= t:
		a += 1
	while 9 ** b <= t:
		b += 1
	a -= 1
	b -= 1
	if t - 6 ** a in d:
		if d[t - 6 ** a] > cnt + 1:
			d[t - 6 ** a] = cnt + 1
			q.append(t - 6 ** a)
	else:
		d[t - 6 ** a] = cnt + 1
		q.append(t - 6 ** a)
	if t - 9 ** b in d:
		if d[t - 9 ** b] > cnt + 1:
			d[t - 9 ** b] = cnt + 1
			q.append(t - 9 ** b)
	else:
		d[t - 9 ** b] = cnt + 1
		q.append(t - 9 ** b)
print(d[0])


N = int(input())
ans = N
for i in range(N + 1):
	cnt = 0
	a = i
	while a > 0:
		cnt += a % 6
		a //= 6
	b = N - i
	while b > 0:
		cnt += b % 9
		b //= 9
	ans = min(ans, cnt)
print(ans)


a = int(input())
k = a
for i in range(a + 1):
	al = 0
	t = i
	while t > 0:
		al += t % 6
		t //= 6
	t = a - i
	while t > 0:
		al += t % 9
		t //= 9
	if al < k:
		k = al
print(k)


import math
N = int(input())
dp = [math.inf for _ in range(N + 1)]
dp[0] = 0
for i in range(1, N + 1):
	p = 1
	q = 1
	while p <= i:
		dp[i] = min(dp[i], dp[i - p] + 1)
		p *= 6
	while q <= i:
		dp[i] = min(dp[i], dp[i - q] + 1)
		q *= 9
print(dp[N])


def Ten_to_n(X, n):
	if int(X / n):
		return Ten_to_n(int(X / n), n) + str(X % n)
	return str(X % n)
n = int(input())
wit = [1]
res = n
for i in range(n + 1):
	cnt = 0
	j = n - i
	si = Ten_to_n(i, 6)
	for i in si:
		cnt += int(i)
	ni = Ten_to_n(j, 9)
	for i in ni:
		cnt += int(i)
	res = min(res, cnt)
print(res)


from math import log
N = int(input())
dp = [0] * (N + 1)
e6 = 0
e9 = 0
for i in range(1, N + 1):
	for j in range(e6, 12):
		if 6 ** j > i:
			e6 = j - 1
			break
	for j in range(e9, 12):
		if 9 ** j > i:
			e9 = j - 1
			break
	dp[i] = min(dp[i - 1], dp[i - int(6 ** e6)], dp[i - int(9 ** e9)]) + 1
print(dp[-1])


n = int(input())
ans = 10 ** 9
for i in range(n + 1):
	tmp = i
	res = 0
	while tmp:
		res += tmp % 6
		tmp //= 6
	tmp = n - i
	while tmp:
		res += tmp % 9
		tmp //= 9
	ans = min(ans, res)
print(ans)


n = int(input())
dp = [100000] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in range(1, 7):
		ind = i - 6 ** j
		if ind >= 0:
			dp[i] = min(dp[i], dp[ind] + 1)
		else:
			break
	for j in range(1, 6):
		ind = i - 9 ** j
		if ind >= 0:
			dp[i] = min(dp[i], dp[ind] + 1)
		else:
			break
	dp[i] = min(dp[i], dp[i - 1] + 1)
print(dp[n])


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def f(n, a):
	ans = 0
	while n > 0:
		ans += n % a
		n //= a
	return ans

def main():
	N = int(readline())
	ans = INF
	for i in range(N + 1):
		ans = min(ans, f(i, 6) + f(N - i, 9))
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


ans = N = int(input())
for i in range(N + 1):
	(a_, n6, n9) = (0, i, N - i)
	while n6 > 0:
		a_ += n6 % 6
		n6 = n6 // 6
	while n9 > 0:
		a_ += n9 % 9
		n9 = n9 // 9
	ans = min(ans, a_)
print(ans)


N = int(input())
ans = 10 ** 5
for a in range(0, N + 1):
	cnt = 0
	t = 1
	for i in range(7):
		cnt += a // t % 6
		t *= 6
	t = 1
	for i in range(6):
		cnt += (N - a) // t % 9
		t *= 9
	ans = min(ans, cnt)
print(ans)


n = int(input())
ans = n
for i in range(n + 1):
	cont = 0
	j = n - i
	while i > 0:
		cont += i % 6
		i //= 6
	while j > 0:
		cont += j % 9
		j //= 9
	ans = min(ans, cont)
print(ans)


n = int(input())
ans = 10 ** 100
for t in range(n + 1):
	p = t
	q = n - p
	c = 0
	while p > 0:
		c += p % 6
		p = p // 6
	while q > 0:
		c += q % 9
		q = q // 9
	ans = min(ans, c)
print(ans)


N = int(input())
c = (1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049)
DP = [N for i in range(N + 1)]
DP[0] = 0
for cost in c:
	for i in range(cost, N + 1):
		DP[i] = min(DP[i], DP[i - cost] + 1)
print(DP[N])


n = int(input())

def func(n, a):
	res = 0
	while n > 0:
		res += n % a
		n //= a
	return res
mini = n
for i in range(n + 1):
	tmp = func(i, 6) + func(n - i, 9)
	mini = min(mini, tmp)
print(mini)


def nsin(X, n):
	if int(X / n):
		return nsin(int(X / n), n) + str(X % n)
	return str(X)
N = int(input())
score = N
for i in range(N + 1):
	score = min(score, sum(map(int, nsin(i, 6) + nsin(N - i, 9))))
print(score)