This page is still under construction.

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

Transformation from One

Time limit1sMemory limit1024 MB

Summary
Starting from 1, you may add 1 to the first or last digit for cost 1, or multiply it by 2..9 for cost 2; find the minimum cost to reach each given number, or -1.
Level

Hard8 of 10

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

Problem

The kingdom of Numeria is very proud of the quality of its numbers, so it charges its citizens a tax for every change made to a number. Even so, the people of Numeria love transforming numbers.

A group of friends, the Units, use the cheapest possible transformations. A number is written in decimal without leading zeros, and only its first (most significant) or last (least significant) digit may be changed:

  • Add one to the first or last digit dd, replacing that digit with the decimal representation of d+1d+1. This costs 11 gold coin. (If d=9d=9, then d+1=10d+1=10, so the single digit "9" is replaced by the two digits "10" and the number grows longer.)
  • Multiply the first or last digit dd by any digit kk from 22 to 99, replacing it with the decimal representation of d×kd \times k. This costs 22 gold coins. (If d×k≥10d \times k \ge 10, that digit is replaced by two digits.)

The Units always start from the number 11.

For example, 20212021 can be obtained from 11 with the following sequence, costing 1414 gold coins:

  1. Add 11 to 11 — we get 22.
  2. Multiply 22 by 55 — we get 1010.
  3. Add 11 to the first digit — we get 2020.
  4. Multiply the first digit by 55 — we get 100100.
  5. Multiply the first digit by 22 — we get 200200.
  6. Add 11 to the last digit — we get 201201.
  7. Multiply the last digit by 55 — we get 205205.
  8. Multiply the last digit by 44 — we get 20202020.
  9. Add 11 to the last digit — we get 20212021.

In the diagram below, the number above each arrow is the cost of that step and the expression below it is the operation applied.

1⟹11+12⟹22×510⟹11+120⟹22×5100⟹21×2200⟹10+1201⟹21×5205⟹25×42020⟹10+120211 \underset{1 +1}{\overset{1}{\Longrightarrow}} 2 \underset{2 \times 5}{\overset{2}{\Longrightarrow}} 10 \underset{1 +1}{\overset{1}{\Longrightarrow}} 20 \underset{2 \times 5}{\overset{2}{\Longrightarrow}} 100 \underset{1 \times 2}{\overset{2}{\Longrightarrow}} 200 \underset{0 +1}{\overset{1}{\Longrightarrow}} 201 \underset{1 \times 5}{\overset{2}{\Longrightarrow}} 205 \underset{5 \times 4}{\overset{2}{\Longrightarrow}} 2020 \underset{0+1}{\overset{1}{\Longrightarrow}} 2021

But 20212021 can also be reached more cheaply, for only 99 gold coins:

1⟹21×99⟹29×545⟹24×5205⟹25×42020⟹10+120211 \underset{1 \times 9}{\overset{2}{\Longrightarrow}} 9 \underset{9 \times 5}{\overset{2}{\Longrightarrow}} 45 \underset{4 \times 5}{\overset{2}{\Longrightarrow}} 205 \underset{5 \times 4}{\overset{2}{\Longrightarrow}} 2020 \underset{0+1}{\overset{1}{\Longrightarrow}} 2021

Help the Units obtain MM given numbers using these transformations.

For each of the MM numbers AiA_i, find the least cost for which the Units can obtain AiA_i starting from 11. If a number cannot be obtained by any sequence of these transformations, its answer is −1-1.

Input

The first line contains an integer MM — the count of numbers in the set. Each of the next MM lines contains one natural number AiA_i (1≤i≤M1 \le i \le M).

Output

Print MM lines. On the ii-th line print the least cost of the unit transformations that produce AiA_i from 11. If no such transformations exist for a given number, print −1-1 on its line.

Constraints

  • 1≤M≤501 \le M \le 50
  • 1≤Ai≤10191 \le A_i \le 10^{19}

Examples3

  1. Example 1

    Input
    3
    1000
    5555
    2021
    
    Expected output
    8
    10
    9
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    9
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Expected output
    0
    1
    2
    2
    2
    2
    2
    2
    2