Under construction

Still being built. Things may change

Transparency

Interview

Time limit6sMemory limit1024 MB

Summary
Given a station graph with uppercase and lowercase edge labels, find the shortest pair of distinct accepted strings whose uppercase letters match, or report -1.
Level

Medium6 of 10

Topics
Graph, Shortest path, String
Solved
No attempts yet

Problem

You must audit a factory that produces several products. Each product follows its own taxation rules. Working out which rules apply is hard, because the products are difficult to tell apart. Your job is to check whether the factory is completely transparent in its production process, that is, whether it can produce two different products that look the same when lowercase letters are ignored.

Final products are strings of uppercase letters ('A' to 'Z') and lowercase letters ('a' to 'z'). Two final products are indistinguishable if they are equal after all lowercase letters are removed. For example, WabXYcz is indistinguishable from gWXfbY, since removing the lowercase letters leaves WXY in both.

A factory is modelled as a set of stations SS, a set of directed transition edges TT with labels, an initial station s0s_0, and a set of packaging stations PP. The initial station is in the set of stations, so s0∈Ss_0 \in S, and P⊆SP \subseteq S. A transition edge is a 3-tuple (s1,σ,s2)(s_1, \sigma, s_2), where s1,s2∈Ss_1, s_2 \in S and σ\sigma is an uppercase or lowercase letter. The edge means that if production is at station s1s_1, appending σ\sigma to the product leaves it at station s2s_2. In other words, there is an edge from s1s_1 to s2s_2 labelled σ\sigma.

A product can be produced if, starting at s0s_0, there is a sequence of edges that ends at a station in PP and whose labels, concatenated in order, form the product. The sequence can be empty if s0∈Ps_0 \in P. For example, the first sample factory can produce AF, FAFB, AbFFAd, and AdydAd. This list is not exhaustive. Each transition can be used an unlimited number of times.

For each station and letter, there is at most one outgoing edge labelled with that letter. That is, (s1,σ,s2),(s1,σ,s3)∈T(s_1, \sigma, s_2), (s_1, \sigma, s_3) \in T implies s2=s3s_2 = s_3. A transition may return to the same station, so (s1,σ,s1)∈T(s_1, \sigma, s_1) \in T is allowed.

Given a factory design, determine whether it can produce two distinct but indistinguishable products. If such a pair exists, report the smallest possible sum of the lengths of the two strings in a pair. If no such pair exists, print −1-1.

Input

The first line has three integers ss (1≤s≤501 \le s \le 50), pp (1≤p≤s1 \le p \le s), and tt (1≤t≤52⋅s1 \le t \le 52 \cdot s): the number of stations, the number of packaging stations, and the number of transitions. Stations are numbered 1 to ss.

Each of the next pp lines contains one integer ii (1≤i≤s1 \le i \le s), a packaging station. All values are distinct.

Each of the next tt lines has the form s1σs2s_1 \sigma s_2, meaning a transition from station s1s_1 to station s2s_2 labelled with the letter σ\sigma, where σ\sigma is an uppercase or lowercase letter. The initial station is always station 1. No two transitions leave the same station with the same letter.

Output

Output one integer. If no pair of distinct, indistinguishable products exists, print −1-1. Otherwise, print the smallest value of ∣a∣+∣b∣|a|+|b|, where aa and bb are strings forming such a pair.

Hint

The factory design from the first sample input. Packaging stations are drawn with double circles.

Examples3

  1. Example 1

    Input
    4 1 8
    3
    1 F 1
    1 A 2
    2 b 1
    2 F 3
    2 d 3
    3 B 3
    3 y 4
    4 d 1
    
    Expected output
    10
    
  2. Example 2

    Input
    4 1 8
    3
    1 F 1
    1 A 2
    2 b 1
    2 F 3
    2 d 3
    3 B 3
    3 y 4
    4 d 4
    
    Expected output
    -1
    
  3. Example 3

    Input
    5 2 5
    1
    5
    1 i 2
    2 c 3
    3 p 4
    4 c 1
    1 I 5
    
    Expected output
    4