#abc099c. C - Strange Bank

C - Strange Bank

Score : 300300 points

问题描述

为限制取款的便利性,某银行规定其客户在单次操作中仅能取出以下金额之一:

  • 1 日元(日本货币)

  • 6 日元、6^2(=36)日元、6^3(=216)日元,...

  • 9 日元、9^2(=81)日元、9^3(=729)日元,...

至少需要进行多少次操作才能精确取出总计 N 日元?

不允许将已取出的资金再次存入。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

  • 11 yen (the currency of Japan)

  • 66 yen, 62(=36)6^2(=36) yen, 63(=216)6^3(=216) yen, ...

  • 99 yen, 92(=81)9^2(=81) yen, 93(=729)9^3(=729) yen, ...

At least how many operations are required to withdraw exactly NN yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

  • 1N1000001 \leq N \leq 100000
  • NN is an integer.

Input

Input is given from Standard Input in the following format:

NN

Output

If at least xx operations are required to withdraw exactly NN yen in total, print xx.

Sample Input 1

127

Sample Output 1

4

By withdrawing 11 yen, 99 yen, 36(=62)36(=6^2) yen and 81(=92)81(=9^2) yen, we can withdraw 127127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 11 yen three times, we can withdraw 33 yen in three operations.

Sample Input 3

44852

Sample Output 3

16

update @ 2024/3/10 17:19:09