e-olymp
Problems

Calculator

Calculator

Ilya's calculator performs two actions: multiplies the current number by three and adds one to it. The calculator now shows the number 1. Help Ilya to determine the smallest number of actions c, after which he will get the number n.

Input

One integer n (10n109).

Output

Print the minimum number of operations c.

Time limit 1 second
Memory limit 128 MiB
Input example #1
1447


Output example #1
16


Author Ентін Й.А.
Source XXVIII олімпіада ліцею інформаційних технологій з інформатики. 8-9 класи
Пляшки з мінеральною водою