Flow

가중 이분 그래프를 k개 이어 붙인 층상 네트워크에서 최대 유량이 수렴하는지 판정하고, 수렴하면 그 극한값을, 아니면 -1을 출력한다.

어려움9그래프최단 경로최소 신장 트리아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

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

You are given a bipartite graph GG with nn vertices in both parts and mm 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\\{F\_k\\}.

Here are the steps to construct the network F_kF\_k.

  • We first produce kk copies of the graph GG. We call these copies G_1,G_2,,G_kG\_1, G\_2, \ldots, G\_k.
  • For all 1ik11 \leq i \leq k - 1 and 1un1 \leq u \leq n, we add a directed edge from uu-th vertex in the right part of G_iG\_i to uu-th vertex in the left part of G_i+1G\_{i + 1} with infinite capacity.
  • We add directed edges from the source to all the vertices in the left part of G_1G\_1 with infinite capacity.
  • We add directed edges from all the vertices in the right part of G_kG\_k to the sink with infinite capacity.

Let f_kf\_k be the maximum flow in the network F_kF\_k.

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

입력

The first line contains two integers nn and mm (1n20001 \leq n \leq 2000, 1m40001 \leq m \leq 4000).

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

출력

If the sequence f_k\\{f\_k\\} does not converge to a constant, output 1-1. Otherwise, output lim_k+f_k\lim\limits\_{k \to +\infty} f\_k.