시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 256 MB55121137.931%

문제

We are considering a maximum flow problem on an infinite network.

You are given a bipartite graph $G$ with $n$ vertices in both parts and $m$ directed edges. Each edge goes from the left part to the right part, and has its capacity specified. We want to construct a family of networks $\{F_k\}$.

Here are the steps to construct the network $F_k$.

  • We first produce $k$ copies of the graph $G$. We call these copies $G_1, G_2, \ldots, G_k$.
  • For all $1 \leq i \leq k - 1$ and $1 \leq u \leq n$, we add a directed edge from $u$-th vertex in the right part of $G_i$ to $u$-th vertex in the left part of $G_{i + 1}$ with infinite capacity.
  • We add directed edges from the source to all the vertices in the left part of $G_1$ with infinite capacity.
  • We add directed edges from all the vertices in the right part of $G_k$ to the sink with infinite capacity.

Let $f_k$ be the maximum flow in the network $F_k$.

We want to know what the sequence $\{f_k\}$ looks like when $k$ goes to infinity. If $\{f_k\}$ does not converge to a constant, output $-1$. Otherwise, output $\lim\limits_{k \to +\infty} f_k$.

입력

The first line contains two integers $n$ and $m$ ($1 \leq n \leq 2000$, $1 \leq m \leq 4000$).

Each of the following $m$ lines contains three integers $u$, $v$, and $w$ ($1 \leq u, v \leq n$, $1 \leq w \leq 10^5$) which indicate that there is a directed edge from the $u$-th vertex in the left part to the $v$-th vertex in the right part with capacity $w$.

출력

If the sequence $\{f_k\}$ does not converge to a constant, output $-1$. Otherwise, output $\lim\limits_{k \to +\infty} f_k$.

예제 입력 1

5 5
1 2 3
2 3 4
3 1 2
4 5 6
5 4 3

예제 출력 1

12