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, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define INF (1LL << 60)
#define MAX_V 5000
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

struct edge { ll to, cap, rev; };

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(ll from, ll to, ll cap) {
  G[from].push_back((edge) { to, cap, (ll) G[to].size() });
  G[to].push_back((edge) { from, cap, (ll) G[from].size() - 1 });
}

ll dfs(ll v, ll t, ll f) {
  if(v == t) return f;
  used[v] = true;
  REP(i, 0, G[v].size()) {
    edge &e = G[v][i];
    if(!used[e.to] && e.cap > 0) {
      ll d = dfs(e.to, t, min(f, e.cap));
      if(d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(ll s, ll t) {
  ll flow = 0;
  while(1) {
    memset(used, 0, sizeof(used));
    ll f = dfs(s, t, INF);
    if(f == 0) return flow;
    flow += f;
  }
}

bool dfs2(ll v, ll s, ll t) {
  if(v == t) return true;
  used[v] = true;
  bool ret = false;
  REP(i, 0, G[v].size()) {
    edge &e = G[v][i];
    if(v == s && e.to == t) continue;
    if(v == t && e.to == s) continue;
    if(!used[e.to] && e.cap > 0) ret = ret || dfs2(e.to, s, t);
  }
  return ret;
}

int main(void) {
  ll V, E;
  cin >> V >> E;
  REP(i, 0, E) {
    ll s, t, c;
    cin >> s >> t >> c;
    add_edge(s, t, c);
  }

  vector<pll> q;
  REP(i, 0, V) REP(j, 0, G[i].size()) if(G[i][j].cap == 1) q.push_back(pll(i, j));

  ll flow = max_flow(0, V - 1);

  bool f = false;
  REP(i, 0, q.size()) {
    ll s = q[i].first;
    ll t = G[s][q[i].second].to;
    memset(used, 0, sizeof(used));
    if(G[s][q[i].second].cap == 0 && !dfs2(s, s, t)) f = true;
  }

  ll ans = max(flow - f, 0LL);
  cout << (ans <= 10000 ? ans : -1) << endl;
}