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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <complex>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

struct Dinic{
  
  struct edge {
    int to;
    ll cap;
    int rev;
    edge(int a, ll b, int c):to(a),cap(b),rev(c){}
  };
  
  vector<vector<edge> > G;
  vector<int> level, iter;
  int MAX_V;
  
  Dinic(int max_v):G(max_v,vector<edge>()),MAX_V(max_v){}
  
  int add_edge(int from,int to,ll cap){
    int id = G[from].size();
    
    G[from].push_back(edge(to,cap,G[to].size()));
    G[to].push_back(edge(from,0,G[from].size() - 1));
    
    return id;
  }
  
  void bfs(int s){
    level.assign(MAX_V,-1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while(q.size()){
      int v = q.front(); q.pop();
      for(int i=0;i<G[v].size();i++){
        edge &e = G[v][i];
        if(e.cap > 0 && level[e.to] < 0){
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
  
  ll dfs(int v, int t, ll f){
    if(v == t) return f;
    for(int &i = iter[v];i < G[v].size(); i++){
      edge &e = G[v][i];
      if(e.cap > 0 && level[v] < level[e.to]){
        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 solve(int s, int t){
    ll flow = 0;
    while(1){
      bfs(s);
      if(level[t] < 0) return flow;
      iter.assign(MAX_V,0);
      ll f;
      while((f = dfs(s, t, INF)) > 0) flow += f;
    }
  }
  
};

int main(){
  int v,e,s,t,c;

  scanf("%d%d",&v,&e);

  Dinic dinic(v);

  ll pow10_8 = 100000000;
  
  for(int i=0;i<e;i++){
    scanf("%d%d%d",&s,&t,&c);

    if(c > 1){
      dinic.add_edge(s, t, c*pow10_8 + c);
      dinic.add_edge(t, s, c*pow10_8 + c);
    }else{
      dinic.add_edge(s, t, pow10_8);
      dinic.add_edge(t, s, pow10_8);
    }
  }

  ll res = dinic.solve(0,v-1);

  ll ans0 = res/pow10_8;
  ll ans1 = res%pow10_8;

  if(ans0 > ans1){
    ans1 = ans0 - 1;
  }
  
  if(ans1 <= 10000LL)
    printf("%lld\n",ans1);
  else
    puts("-1");
  
  return 0;
}