Problem: problem

There are $ V $ islands, numbered $ 0, 1, ..., V-1 $, respectively. There are $ E $ bridges, numbered $ 0, 1, ..., E-1 $, respectively. The $ i $ th bridge spans island $ s_i $ and island $ t_i $ and is $ c_i $ wide.

The AOR Ika-chan Corps (commonly known as the Squid Corps), which has a base on the island $ 0 $, is small in scale, so it is always being killed by the Sosu-Usa Corps (commonly known as the Sosuusa Corps) on the island $ V-1 $. .. One day, the squid group got the information that "the Sosusa group will attack tomorrow." A large number of Sosusa's subordinates move across the island from the Sosusa's base, and if even one of their subordinates reaches the Squid's base, the Squid will perish ...

Therefore, the squid group tried to avoid the crisis by putting a road closure tape on the bridge to prevent it from passing through. The length of the tape used is the sum of the widths of the closed bridges. Also, if all the subordinates of the Sosusa group always pass through a bridge with a width of $ 1 $, the squid group can ambush there to prevent them from passing through the Sosusa group.

The length of the tape it holds is $ 10 ^ 4 $. Considering the future, I would like to consume as little tape as possible. Find the minimum length of tape used to keep Sosusa's subordinates from reaching the squid's base. However, if the tape is not long enough and you are attacked by all means, output $ -1 $.



output

Output the minimum length of tape used to prevent Sosusa's subordinates from reaching the Squid's base. However, if the tape is not long enough and you are attacked by all means, output $ -1 $. Also, output a line break at the end.

Example

Input

4 4
0 1 3
0 2 4
1 3 1
2 3 5


Output

4

Solution (in CPP):

Solution:
#include  <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;

const int vmax = 5010;
const int inf = 1 << 29;
struct edge {int to, cap, rev; bool original;};
vector<edge> graph[vmax];
int level[vmax], iter[vmax];

void add_edge(int from, int to, int cap) {
	graph[from].push_back({to, cap, (int)graph[to].size(), true});
	graph[to].push_back({from, 0, (int)graph[from].size() - 1, false});
}

int dfs(int v, int t, int f) {
	if (v == t) return f;
	for (int &i = iter[v]; i < graph[v].size(); i++) {
		edge &e = graph[v][i];
		if (e.cap > 0 && level[v] < level[e.to]) {
			int d = dfs(e.to, t, min(f, e.cap));
			if (d == 0) continue;
			e.cap -= d; graph[e.to][e.rev].cap += d;
			return d;
		}
	}
	return 0;
}

int bfs(int s, int t) {
	rep(i, vmax) level[i] = -1;
	queue<int> q;
	level[s] = 0, q.push(s);
	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (auto &e : graph[v]) {
			if (e.cap > 0 && level[e.to] < 0) {
				level[e.to] = level[v] + 1;
				q.push(e.to);
			}
		}
	}
	return level[t] >= 0;
}


int max_flow(int s, int t) {
	int flow = 0;
	while (bfs(s, t)) {
		rep(i, vmax) iter[i] = 0;
		while (1) {
			int f = dfs(s, t, inf);
			if (f == 0) break;
			flow += f;
		}
	}
	return flow;
}

int flow(int s, int t) {
	if (!bfs(s, t)) return 0;
	rep(i, vmax) iter[i] = 0;
	return dfs(s, t, 1);
}

int add(int from, int to, int s, int t) {
	add_edge(from, to, 1);
	add_edge(to, from, 1);
	return flow(s, t);
}

int erase(int from, int to, int s, int t) {
	int ft = 0;
	for (auto &e : graph[from]) {
		if (e.to == to && e.original) {
			if (graph[e.to][e.rev].cap == 1) ft++;
			e.cap = 0, graph[e.to][e.rev].cap = 0;
		}
	}

	for (auto &e : graph[to]) {
		if (e.to == from && e.original) {
			if (graph[e.to][e.rev].cap == 1) ft--;
			e.cap = 0, graph[e.to][e.rev].cap = 0;
		}
	}

	if (ft == 0) return 0;

	if (ft > 0 && flow(from, to) == 1) return 0;
	if (ft < 0 && flow(to, from) == 1) return 0;

	if (ft > 0)flow(t, to), flow(from, s);
	if (ft < 0)flow(t, from), flow(to, s);
	return -1;
}


void show(int n) {
	rep(i, n)for (auto &e : graph[i]) if (e.original)
			cout << i << " -> " <<  e.to << " : " << graph[e.to][e.rev].cap << endl;
}

int a[5010], b[5010], c[5010];


int main(void) {
	int n, m;
	cin >> n >> m;

	rep(i, m) {
		cin >> a[i] >> b[i] >> c[i];
		add_edge(a[i] , b[i] , c[i]);
		add_edge(b[i] , a[i] , c[i]);
	}

	int cur = max_flow(0, n - 1);
	int ans = cur;

	rep(i, m) {
		if (c[i] != 1) continue;
		if (erase(a[i] , b[i] , 0, n - 1) == -1) ans = min(ans, cur - 1);
		add(a[i] , b[i] , 0, n - 1);
	}

	if (ans > 10000) ans = -1;
	cout << ans << endl;

	return 0;
}