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 <cstring>
#include <algorithm>

using int64 = long long;

namespace NetFlow {
  const int N = 5e3 + 10, MAXM = 1e5 + 10;
  const int64 inf = 1ll << 60;
  struct Edge {
    int v,nx;//c:capcity, f:flow
    int64 c, f;
    Edge() {}
    Edge(int v,int64 c,int64 f,int nx):v(v),c(c),f(f),nx(nx) {}
  } E[MAXM];
  int G[N],cur[N],pre[N],gap[N],n,sz;
  int64 dis[N];
  void init(int _n) {
    n=_n,sz=0; memset(G,-1,sizeof(G[0])*n);
  }
  void link(int u,int v,int64 c) {
    E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
    E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
  }
  int64 ISAP(int S,int T) {//S -> T
    int64 maxflow=0,aug=inf;
    int flag=false,u,v;
    for (int i=0;i<n;++i)cur[i]=G[i],gap[i]=dis[i]=0;
    for (gap[S]=n,u=pre[S]=S;dis[S]<n;flag=false) {
      for (int &it=cur[u];~it;it=E[it].nx) {
        if (E[it].c>E[it].f&&dis[u]==dis[v=E[it].v]+1) {
          if (aug>E[it].c-E[it].f) aug=E[it].c-E[it].f;
          pre[v]=u,u=v; flag=true;
          if (u==T) {
            for (maxflow+=aug;u!=S;) {
              E[cur[u=pre[u]]].f+=aug;
              E[cur[u]^1].f-=aug;
            }
            aug=inf;
          }
          break;
        }
      }
      if (flag) continue;
      int mx=n;
      for (int it=G[u];~it;it=E[it].nx) {
        if (E[it].c>E[it].f&&dis[E[it].v]<mx) {
          mx=dis[E[it].v]; cur[u]=it;
        }
      }
      if ((--gap[dis[u]])==0) break;
      ++gap[dis[u]=mx+1]; u=pre[u];
    }
    return maxflow;
  }
  bool bfs(int S,int T) {
    static int Q[N];
    memset(dis,-1,sizeof(dis[0])*n);
    dis[S]=0; Q[0]=S;
    for (int h=0,t=1,u,v,it;h<t;++h) {
      for (u=Q[h],it=G[u];~it;it=E[it].nx) {
        if (dis[v=E[it].v]==-1&&E[it].c>E[it].f) {
          dis[v]=dis[u]+1; Q[t++]=v;
        }
      }
    }
    return dis[T]!=-1;
  }
  int64 dfs(int u,int T,int64 low) {
    if (u==T) return low;
    int64 ret=0,tmp;
    int v;
    for (int &it=cur[u];~it&&ret<low;it=E[it].nx) {
      if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f) {
        if (tmp=dfs(v,T,std::min(low-ret,E[it].c-E[it].f))) {
          ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
        }
      }
    }
    if (!ret) dis[u]=-1; return ret;
  }
  int64 dinic(int S,int T) {
    int64 maxflow=0,tmp;
    while (bfs(S,T)) {
      memcpy(cur,G,sizeof(G[0])*n);
      while (tmp=dfs(S,T,inf)) maxflow+=tmp;
    }
    return maxflow;
  }
}

namespace nf = NetFlow;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  nf::init(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    int64 w;
    scanf("%d%d%lld", &u, &v, &w);
    if (w == 1) w = 1000000000 - 1;
    else w *= 1000000000;
    nf::link(u, v, w);
    nf::link(v, u, w);
  }
  int64 cost = nf::ISAP(0, n - 1);
  cost = cost / 1000000000;
  if (cost > 10000) cost = -1;
  printf("%d\n", (int)cost);
  return 0;
}