Old teletext

In this problem, we use a simplified model of teletext, where the available pages are 100–899, the initial page is always 100, and there are only two ways to move to any page:

Professor Oak’s television is so old, that some keys of its remote control are broken. For instance, assume that the keys ‘1’ and ‘-’ are broken. If he wants to change from page 140 to page 211, he cannot press ‘211’ directly. Still, he can just press ‘+’ 71 times. Alternatively, he can press ‘200’, and afterwards press ‘+’ 11 times, for a total cost of 14. For this example, the minimum cost is 5: first press ‘209’; then press ‘+’ twice.

Given a list of broken keys, and a list of teletext pages that must be visited, which is the minimum cost to visit all those pages at least once, in any order?

Input

Input consists of at most 100 cases. Every case has two lines. The first line has a string with all the broken keys in any order, following exactly the sample input. The second line has a number pp, followed by pp page numbers with no repetitions. Assume 1p91 \le p \le 9.

Output

For every case, print the minimum cost. Print “no solution” if there is none.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:57:25.233Z

© Jutge.org, 2006–2026.
https://jutge.org