#P1400F. x-prime Substrings

    ID: 3005 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>brute forcedfs and similardpstring suffix structuresstrings*2800

x-prime Substrings

Description

You are given an integer value xx and a string ss consisting of digits from 11 to 99 inclusive.

A substring of a string is a contiguous subsequence of that string.

Let f(l,r)f(l, r) be the sum of digits of a substring s[l..r]s[l..r].

Let's call substring s[l1..r1]s[l_1..r_1] xx-prime if

  • f(l1,r1)=xf(l_1, r_1) = x;
  • there are no values l2,r2l_2, r_2 such that
    • l1l2r2r1l_1 \le l_2 \le r_2 \le r_1;
    • f(l2,r2)xf(l_2, r_2) \neq x;
    • xx is divisible by f(l2,r2)f(l_2, r_2).

You are allowed to erase some characters from the string. If you erase a character, the two resulting parts of the string are concatenated without changing their order.

What is the minimum number of characters you should erase from the string so that there are no xx-prime substrings in it? If there are no xx-prime substrings in the given string ss, then print 00.

The first line contains a string ss (1s10001 \le |s| \le 1000). ss contains only digits from 11 to 99 inclusive.

The second line contains an integer xx (1x201 \le x \le 20).

Print a single integer — the minimum number of characters you should erase from the string so that there are no xx-prime substrings in it. If there are no xx-prime substrings in the given string ss, then print 00.

Input

The first line contains a string ss (1s10001 \le |s| \le 1000). ss contains only digits from 11 to 99 inclusive.

The second line contains an integer xx (1x201 \le x \le 20).

Output

Print a single integer — the minimum number of characters you should erase from the string so that there are no xx-prime substrings in it. If there are no xx-prime substrings in the given string ss, then print 00.

Sample Input 1

116285317
8

Sample Output 1

2

Sample Input 2

314159265359
1

Sample Output 2

2

Sample Input 3

13
13

Sample Output 3

0

Sample Input 4

3434343434
7

Sample Output 4

5

Note

In the first example there are two 88-prime substrings "8" and "53". You can erase these characters to get rid of both: "116285317". The resulting string "1162317" contains no 88-prime substrings. Removing these characters is also a valid answer: "116285317".

In the second example you just have to erase both ones.

In the third example there are no 1313-prime substrings. There are no substrings with the sum of digits equal to 1313 at all.

In the fourth example you can have neither "34", nor "43" in a string. Thus, you have to erase either all threes or all fours. There are 55 of each of them, so it doesn't matter which.