Transparency

아직 제출이 없습니다시간 제한6초메모리 제한1024 MB

문제

You must audit a factory that produces a number of products. Each product is subject to its own taxation rules and regulations. Unfortunately, deciding which rules apply to a product is not obvious since it can be difficult to distinguish the products from one another. It is your job to investigate if the factory is being completely transparent in its production process by determining if the factory is capable of producing two different products that are indistinguishable from one another.

The final products are represented by sequences of uppercase letters ('A'--'Z') and lowercase letters ('a'--'z'). Two final products are indistinguishable from one another if the products are equal after removing all lowercase letters. For example, WabXYcz is indistinguishable from gWXfbY, since after removing all lowercase letters we are left with WXY in both instances.

A factory is modelled as a set of stations SS, a set of directed and labelled transition edges TT, an initial station s_0s\_0, and a set of packaging stations PP. The initial station is in the set of stations, i.e., s_0Ss\_0\in S, and the set of packaging stations is a subset of SS, i.e., PSP \subseteq S. A transition edge is defined by a 33-tuple (s_1,σ,s_2)(s\_1, \sigma, s\_2), where s_1,s_2Ss\_1,s\_2\in S are stations, and σ\sigma is an uppercase or lowercase letter. The existence of the transition (s_1,σ,s_2)(s\_1, \sigma, s\_2) in TT means that if in producing some product we are at station s_1s\_1, then appending σ\sigma to the product will leave us at station s_2s\_2. That is, there is a directed edge from station s_1s\_1 to station s_2s\_2, labelled σ\sigma.

A product can be produced by the factory if by starting at station s_0s\_0 there is a sequence of edges that can be followed, ending at a station in PP, whose edge labels can be concatenated, in order, to create the product. The sequence of edges can be empty if s_0Ps\_0 \in P. For example, the following strings can all be produced by the factory described in the first sample input and illustrated in the figure: AF, FAFB, AbFFAd, AdydAd. Note that this is not an exhaustive list.

Each production transition can be used an unlimited number of times to make a product. It is guaranteed that for each station, ss, and letter, σ\sigma, there is at most one directed edge from station ss labelled σ\sigma. That is, (s_1,σ,s_2),(s_1,σ,s_3)T(s\_1, \sigma, s\_2), (s\_1, \sigma, s\_3)\in T implies s_2=s_3s\_2=s\_3. It is possible that transitions will go back to the same station; that is, (s_1,σ,s_1)T(s\_1, \sigma, s\_1)\in T is allowed.

Given a factory design, determine if the factory is capable of producing two distinct but indistinguishable products. If there is such a pair of products, then report the sum of the lengths of the strings in the pair that minimizes the sum of the lengths. If there is no such pair, print 1-1.

입력

The first line of input contains three integers, ss (1s501 \le s \le 50), pp (1ps1 \le p \le s) and tt (1t52s1 \le t \le 52 \cdot s), where ss is the number of stations, pp is the number of packaging stations, and tt is the number of transitions. The set of stations is numbered from 11 to ss.

Each of the next pp lines contains a single integer ii (1is1 \le i \le s). These are the packing stations. All values of ii are distinct.

Each of the next tt lines is of the form:

s_1σs_2s\_1 \sigma s\_2

where s_1s\_1 and s_2s\_2 (1s_1,s_2s1 \le s\_1, s\_2 \le s) are stations, and σ\sigma is a single character, consisting of an uppercase or lowercase letter. These are the transitions. The initial station is always station 11. There will never be two transitions out of the same state labelled with the same character.

출력

Output a single line with a single integer. If there is no pair of distinct, indistinguishable products, then output 1-1. If there exists a pair of distinct, indistinguishable products, then output the smallest sum a+b|a|+|b| where aa and bb are strings corresponding to distinct, indistinguishable products.

힌트

The factory design from the first sample input. The packaging station is marked by a double circle.