Ciphertext

주어진 접두사 부호로 문자열 s를 부호화한 뒤, 어떤 조각도 어떤 문자열의 올바른 부호화가 되지 않도록 이진 암호문을 최대 개수로 자른다.

보통7문자열동적 계획법트라이그리디아직 제출이 없습니다시간 제한1초메모리 제한256 MB

문제

Stierlitz wants to deliver a very important message ss to the headquarters. He uses a prefix code defined in the National Standard. Unfortunately, the enemy knows the code, and the communication channel is tapped. In order to avoid suspicion, Stierlitz wants to split the ciphertext into several parts in such a way that none of the parts is a valid encoding of some string using the code. In order to maximize security, Stierlitz wants to maximize the number of parts that he will split the ciphertext into. Find this maximum number of parts, or determine that it is impossible to split the ciphertext satisfying the requirements.

입력

The first line of input contains one integer kk (1k521 \le k \le 52): the size of the alphabet. The possible characters are enumerated from 11 to 5252 in A-Za-z order, and the text of the message will only contain characters with numbers from 11 to kk.

The second line of input contains a non-empty string ss of size up to 10610^{6} which consists of characters with indices from 11 to kk (see enumeration rules described above).

The next kk lines contain binary codes of alphabet characters according to the enumeration order. Each character is encoded by a non-empty sequence of 00s and 11s of length at most kk. It is guaranteed that no character has a code that is equal to the prefix of the code for a different character.

출력

Print a single integer: the maximum number of parts, or -1 in case it is impossible to split the ciphertext into parts satisfying the requirements.

힌트

In the first example, the encoded text looks like 0010110011. The only way to split it into parts which are not valid encodings of any strings is 0 010110011. Therefore, the answer is 22.

In the second example, it is possible to prove by induction that any string ending with 00 and not having three 11s in a row is a valid encoding of some string. Every suffix of the encoded text matches this criterion, therefore the answer is -1.