Roger likes saying yabe. Some people think he might be trying to say eBay, but obviously, he's saying yabe.
Two strings are equivalent if the two strings are equal after removing all lowercase letters.
Given a graph of
nodes and
labelled directed edges with
destination nodes, we define
to be the
language of the graph. Imagine a walk that starts at node
and ends at some destination node.
Let
be the string obtained by concatenating all the labels of the edges that are walked in order.
is thus defined
as all possible strings
that can be generated by this process.
Compute the minimum possible value of where
and
are both in
,
, and
and
are equivalent.
Constraints
If two edges go out of a common vertex, they are labelled with distinct letters.
Input Specification
The first line contains three integers, ,
, and
.
Each of the next lines contains a unique positive integer less than or equal to
. These represent the destination nodes.
Each of the next lines contains an integer
, a lowercase or uppercase letter
, and another integer
, indicating
a directed edge labeled with
going from vertex
to vertex
.
Output Specification
Output the minimum possible value of where
and
are both in
,
, and
and
are equivalent.
If no such value exists, output
-1
.
Sample 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
Sample Output
10
Comments