This page is still under construction.

Parts of this page are still being built. What you see may change.

Unit Transformation

Time limit1sMemory limit1024 MB

Summary
Find the minimum cost to build a target number from 1 using only operations on the last digit.
Level

Medium6 of 10

Topics
Dynamic programming, BFS, Implementation, Math
Solved
No attempts yet

Problem

The kingdom of Numeracija takes great pride in the quality of its numbers, so it collects a tax from its citizens for every change made to a number. Even so, the citizens of Numeracija love transforming numbers.

A group of friends called the Vienetukai ("the Ones") love transforming numbers, always starting from the number 11. Because they are not wealthy, they use only the cheapest transformations, which act only on the last (least significant) digit:

  • add 11 to the last digit of the number — costs 11 gold;
  • multiply the last digit of the number by any integer from 22 to 99 — costs 22 gold.

A transformation always acts on the single last digit, and its result takes the place of that digit (so a two-digit product simply lengthens the number). For example, multiplying the last digit of 77 by 33 turns 77 into 2121, and multiplying the last digit of 2525 (which is 55) by 33 replaces 55 with 1515, giving 215215.

For example, using these operations the number 21212121 can be obtained from 11 by the following sequence of transformations:

  1. Multiply 11 by 77 to get 77.
  2. Multiply 77 by 33 to get 2121.
  3. Add 11 to the last digit to get 2222.
  4. Multiply the last digit by 55 to get 210210.
  5. Add 11 to the last digit to get 211211.
  6. Multiply the last digit by 33 to get 213213.
  7. Multiply the last digit by 77 to get 21212121.

This transformation costs 1212 gold and can be shown schematically as:

1⟹21×77⟹27×321⟹11+122⟹22×5210⟹10+1211⟹21×3213⟹23×721211 \underset{1 \times 7}{\overset{2}{\Longrightarrow}} 7 \underset{7 \times 3}{\overset{2}{\Longrightarrow}} 21 \underset{1 +1}{\overset{1}{\Longrightarrow}} 22 \underset{2 \times 5}{\overset{2}{\Longrightarrow}} 210 \underset{0 + 1}{\overset{1}{\Longrightarrow}} 211 \underset{1 \times 3}{\overset{2}{\Longrightarrow}} 213 \underset{3 \times 7}{\overset{2}{\Longrightarrow}} 2121

The number 21212121 could also be obtained more cheaply, for only 99 gold:

1⟹21×55⟹25×525⟹25×3215⟹25×42120⟹10+121211 \underset{1 \times 5}{\overset{2}{\Longrightarrow}} 5 \underset{5 \times 5}{\overset{2}{\Longrightarrow}} 25 \underset{5 \times 3}{\overset{2}{\Longrightarrow}} 215 \underset{5 \times 4}{\overset{2}{\Longrightarrow}} 2120 \underset{0 +1 }{\overset{1}{\Longrightarrow}} 2121

Help the Vienetukai save money: find the minimum cost for which they can obtain the given number AA from 11 using the described transformations.

Input

The first line contains a natural number AA.

Output

Output a single integer — the minimum cost for which the Vienetukai can obtain the given number AA from 11. If AA cannot be obtained using the described transformations, output −1-1.

Constraints

  • 1≤A≤1091 \le A \le 10^9

Examples3

  1. Example 1

    Input
    1000
    
    Expected output
    -1
    
  2. Example 2

    Input
    2121
    
    Expected output
    9
    
  3. Example 3

    Input
    5555
    
    Expected output
    10