Mirko is a truck driver. His job is to travel between cities by roads, loading and unloading the cargo. His truck is so big that it can load an unlimited number of packages, but the automated loading system enables only the last loaded package to be unloaded. There exist
The cities are connected by one-way roads of length
- Type
– each time Mirko drives down the road of this type, he must load exactly one package of the appropriate type for that road. - Type
– each time Mirko drives down the road of this type, he must unload exactly one package of the appropriate type for that road. - Type
– Mirko can drive down the road of this type without loading or unloading packages (no loading/unloading).
Mirko is required not to load/unload any cargo except when driving down the roads of type
Mirko can travel along
Write a program which computes the number of different ways that Mirko can travel so that he traverses at most
Input Specification
The first line of input contains positive integers
The following
- Type
–x y C
, where and are positive integers which describe the direction of the road and is an uppercase letter of the English alphabet which denotes the type of package that Mirko must load onto the truck. - Type
–x y c
, where and are positive integers which describe the direction of the road and is a lowercase letter of the English alphabet which denotes the type of package that Mirko must unload from the truck. - Type
–x y
, where and are positive integers which describe the direction of the road.
In the above formats, the road is traversable when traveling from city
Output Specification
In a single line of output, print the number of different ways Mirko can arrive in city
Sample Input 1
2 1 10
1 2 a
Sample Output 1
0
Sample Input 2
7 9 5
1 2 A
2 3 B
2 5
5 3 C
3 4 b
3 6 c
3 7
4 7 a
6 7 a
Sample Output 2
4
Comments