CF BUDDY
← Problems·

1681D · Required Length

1700 · brute force, dfs and similar, dp

Problem: You are given two integer numbers, nn and xx. You may perform several operations with the integer xx.

Each operation you perform is the following one: choose any digit yy that occurs in the decimal representation of xx at least once, and replace xx by xyx \cdot y.

You want to make the length of decimal representation of xx (without leading zeroes) equal to nn. What is the minimum number of operations required to do that?

Input Format: The only line of the input contains two integers nn and xx (2n192 \le n \le 19; 1x<10n11 \le x < 10^{n-1}).

Output Format: Print one integer — the minimum number of operations required to make the length of decimal representation of xx (without leading zeroes) equal to nn, or 1-1 if it is impossible.

Note: In the second example, the following sequence of operations achieves the goal:

  1. multiply xx by 22, so x=22=4x = 2 \cdot 2 = 4;
  2. multiply xx by 44, so x=44=16x = 4 \cdot 4 = 16;
  3. multiply xx by 66, so x=166=96x = 16 \cdot 6 = 96;
  4. multiply xx by 99, so x=969=864x = 96 \cdot 9 = 864.

Sample Cases

Case 1

Input

2 1

Output

-1

Case 2

Input

3 2

Output

4

Case 3

Input

13 42

Output

12

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback