Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/dp-birgacha-amallar

Fewest operations to one

Easy 1000 ms 128 MB 23%
Share

A positive integer $n$ is given. In one operation you may subtract $1$ from the number, divide it by $2$ if it is even, or divide it by $3$ if it is divisible by $3$. What is the smallest number of operations needed to turn $n$ into $1$?

A number and the shortest chain of operations from it down to one

Input

One positive integer $n$.

Output

One integer — the smallest number of operations.

Note

$1 \le n \le 2 * 10^{5}$

Examples

Input #1
10
Output #1
3
Input #2
1
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.