Description

Hard

The DNA Sequence Splitter

A bioinformatician needs to split a DNA sequence into valid gene segments. The DNA sequence contains only characters 'A', 'C', 'G', and 'T'.

A segment is valid if it matches one of the known gene patterns. You are given k known patterns.

Find the minimum number of segments needed to completely split the DNA sequence using only valid patterns. Patterns can be reused.

If the sequence cannot be completely split, return -1.

Input Format

The first line contains the DNA sequence string s. The second line contains an integer k, the number of known patterns. The next k lines each contain a pattern string.

Output Format

Print the minimum number of segments, or -1 if impossible.

Examples

Example 1

Input
ACGTACGT 2 ACGT ACG
Output
2
ExplanationSplit as ACGT + ACGT = 2 segments.

Example 2

Input
AACCGG 3 AA CC GG
Output
3
ExplanationSplit as AA + CC + GG = 3 segments.

Constraints

•

1 ≤ length of s ≤ 1000

•

1 ≤ k ≤ 100

•

1 ≤ length of each pattern ≤ 100

•

All strings contain only A, C, G, T

Code

Login
Error

Case 1

Case 2

Input
ACGTACGT 2 ACGT ACG
Output
No output
Expected
2