Problem:
Problem G. Additions

You are given an integer $N$ and a string consisting of '+' and digits. You are asked to transform the string into a valid formula whose calculation result is smaller than or equal to $N$ by modifying some characters. Here, you replace one character with another character any number of times, and the converted string should still consist of '+' and digits. Note that leading zeros and unary positive are prohibited.

For instance, '0123+456' is assumed as invalid because leading zero is prohibited. Similarly, '+1+2' and '2++3' are also invalid as they each contain a unary expression. On the other hand, '12345', '0+1+2' and '1234+0+0' are all valid.

Your task is to find the minimum number of the replaced characters. If there is no way to make a valid formula smaller than or equal to $N$, output $-1$ instead of the number of the replaced characters.

Input

The input consists of a single test case in the following format.

$N$
$S$

The first line contains an integer $N$, which is the upper limit of the formula ($1 \leq N \leq 10^9$). The second line contains a string $S$, which consists of '+' and digits and whose length is between $1$ and $1,000$, inclusive. Note that it is not guaranteed that initially $S$ is a valid formula.

Output

Output the minimized number of the replaced characters. If there is no way to replace, output $-1$ instead.

Examples

Sample Input 1

100
+123

Output for Sample Input 1

2

Sample Input 2

10
+123

Output for Sample Input 2

4

Sample Input 3

1
+123

Output for Sample Input 3

-1

Sample Input 4

10
++1+

Output for Sample Input 4

2

Sample Input 5

2000
1234++7890

Output for Sample Input 5

2

In the first example, you can modify the first two characters and make a formula '1+23', for instance. In the second example, you should make '0+10' or '10+0' by replacing all the characters. In the third example, you cannot make any valid formula less than or equal to $1$.

Solution in Kotlin (judge verdict: Accepted)

fun Boolean.toInt(): Int = if (this) 1 else 0
fun main(args: Array<String>?): Unit {
    val n = readLine()!!.trim().toLong()
    val s = readLine()!!.trim()
    val memo = Array(s.length){i -> Array(2){Array(11){LongArray(s.length - i){-1L}}}}
    val digit: LongArray = LongArray(10){(0 until it).fold(1L){a, _ -> a * 10}}
    fun sub(i: Int, zeroAllow: Boolean, d: Int, c: Int): Long {
        if (c < 0) return n + 1L
        if (i == s.length) return if (d == 10) 0L else n + 1L
        if (c >= s.length - i) {
            return when(d) {
                10 -> if (s.length - i == 1) n + 1L else if ((s.length - i) % 2 == 0) 0L else 10L
                0 -> sub(i + 1, zeroAllow, 10, c - 1)
                else -> if (zeroAllow) sub(i + 1, true, d - 1, c - 1) else sub(i + 1, true, d - 1, c - 1) + digit[d]
            }
        }
        if (memo[i][zeroAllow.toInt()][d][c] >= 0) return memo[i][zeroAllow.toInt()][d][c]
        memo[i][zeroAllow.toInt()][d][c] = when(d) {
            10 -> when(s[i]){
                '+' -> (0..9).map{j -> sub(i + 1, false, j, c)}.min()!!
                else -> (0..9).map{j -> sub(i + 1, false, j, c - 1)}.min()!!
            }
            0 -> when(s[i]) {
                '+' -> sub(i + 1, false,10, c - 1)
                '0' -> sub(i + 1, false, 10, c)
                else -> Math.min(sub(i + 1, false, 10, c - 1), sub(i + 1, false, 10, c) + (s[i] - '0'))
            }
            else -> when(s[i]) {
                '+' -> if (zeroAllow) sub(i + 1, true, d - 1, c - 1) else (sub(i + 1, true, d - 1, c - 1) + digit[d])
                '0' -> if (zeroAllow) sub(i + 1, true, d - 1, c) else (sub(i + 1, true, d - 1, c - 1) + digit[d])
                else -> if (zeroAllow) Math.min(sub(i + 1, true, d - 1, c - 1), sub(i + 1, true, d - 1, c) + (s[i] - '0') * digit[d]) else Math.min(sub(i + 1, true, d - 1, c - 1) + digit[d], sub(i + 1, true, d - 1, c) + (s[i] - '0') * digit[d])
            }
        }
        if (memo[i][zeroAllow.toInt()][d][c] > n) {
            memo[i][zeroAllow.toInt()][d][c] = n + 1L
        }
        return memo[i][zeroAllow.toInt()][d][c]
    }
    println((0 .. s.length).filter{c -> (0 .. 9).map{d -> sub(0, false, d, c)}.any{it <= n}}.min() ?: -1)
}