Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

brute force

dfs and similar

dp

string suffix structures

strings

*2800

No tag edit access

The problem statement has recently been changed. View the changes.

×
F. x-prime Substrings

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputYou are given an integer value $$$x$$$ and a string $$$s$$$ consisting of digits from $$$1$$$ to $$$9$$$ inclusive.

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

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

Let's call substring $$$s[l_1..r_1]$$$ $$$x$$$-prime if

- $$$f(l_1, r_1) = x$$$;
- there are no values $$$l_2, r_2$$$ such that
- $$$l_1 \le l_2 \le r_2 \le r_1$$$;
- $$$f(l_2, r_2) \neq x$$$;
- $$$x$$$ is divisible by $$$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 $$$x$$$-prime substrings in it? If there are no $$$x$$$-prime substrings in the given string $$$s$$$, then print $$$0$$$.

Input

The first line contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$). $$$s$$$ contains only digits from $$$1$$$ to $$$9$$$ inclusive.

The second line contains an integer $$$x$$$ ($$$1 \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 $$$x$$$-prime substrings in it. If there are no $$$x$$$-prime substrings in the given string $$$s$$$, then print $$$0$$$.

Examples

Input

116285317 8

Output

2

Input

314159265359 1

Output

2

Input

13 13

Output

0

Input

3434343434 7

Output

5

Note

In the first example there are two $$$8$$$-prime substrings "8" and "53". You can erase these characters to get rid of both: "116285317". The resulting string "1162317" contains no $$$8$$$-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 $$$13$$$-prime substrings. There are no substrings with the sum of digits equal to $$$13$$$ 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 $$$5$$$ of each of them, so it doesn't matter which.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/09/2024 05:18:15 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|