F. Forbidden Spell Sequence
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
Example
Input
3 2
2 a b
3 b c d
Output
301