You are given a directed graph with $$$n$$$ vertices and $$$m$$$ edges. Each edge is labeled by a lowercase English letter.
A path in the graph is any sequence of edges [ e_1, e_2, ..., e_k ] such that the ending vertex of $$$e_i$$$ is the starting vertex of $$$e_{i+1}$$$ for every $$$1 \le i \lt k$$$. Vertices and edges may be visited multiple times.
Let the string written on a path be the string of length $$$k$$$ formed by the letters on its edges in order.
A path is called good if its string is not a palindrome.
Your task is to determine whether there exist arbitrarily long good paths. If yes, report that the answer is infinite. Otherwise, find the maximum possible length of a good path.
Recall that a string $$$s$$$ is a palindrome if it reads the same from left to right and from right to left.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of vertices and edges.
Each of the next $$$m$$$ lines contains two integers $$$u$$$, $$$v$$$ and a lowercase English letter $$$c$$$ ($$$1 \le u, v \le n$$$), describing a directed edge from vertex $$$u$$$ to vertex $$$v$$$ labeled by the letter $$$c$$$.
Multiple edges and self-loops are allowed.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of vertices and edges.
Each of the next $$$m$$$ lines contains two integers $$$u$$$, $$$v$$$ and a lowercase English letter $$$c$$$ ($$$1 \le u, v \le n$$$), describing a directed edge from vertex $$$u$$$ to vertex $$$v$$$ labeled by the letter $$$c$$$.
Multiple edges and self-loops are allowed.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
| Подзадача | Баллы | Доп. ограничения | Необх. подзадачи |
| 1 | 4 | $$$n, m \le 5$$$ | — |
| 2 | 7 | $$$n, m \le 20$$$, алфавит бинарный | 1 |
| 3 | 8 | $$$n, m \le 50$$$ | 1 |
| 4 | 10 | $$$n, m \le 200$$$ | 1–3 |
| 5 | 8 | либо $$$\mathrm{ans}$$$ нечётно, либо $$$\mathrm{ans}=0$$$, либо ответ равен $$$-1$$$ | 1 |
| 6 | 14 | $$$n, m \le 3000$$$ | 1–5 |
| 7 | 15 | $$$u_i \lt v_i$$$ для всех рёбер $$$(u_i, v_i)$$$ | 3, 4, 6 |
| 8 | 14 | алфавит бинарный | 2, 4, 6 |
| 9 | 20 | нет дополнительных ограничений | 1–8 |
11 11 1 a
0
14 31 2 a2 3 b3 4 c
3
15 41 2 a2 3 b3 4 b4 5 a
3
12 21 2 b2 2 a
-1
For each test case:
In case there exist good paths of arbitrarily large length print $$$-1$$$.
In case paths don't exist at all, print $$$0$$$.
Otherwise, print $$$x$$$, where $$$x$$$ is the maximum length of a good path.