#include<bits/stdc++.h>
using namespace std;
vector<int> dist;
vector<int> ptr;
vector< vector<int> > graph(5000);
long long cap[5009][5009];
long long flow[5009][5009];
int n;
bool bfs(int source, int sink)
{
queue<int> q;
vector<int> visited(n,false);
int i,j,v;
dist.resize(n);
ptr.resize(n);
for(i=0;i<n;i++)
dist[i]= ptr[i]= 0;
q.push(source);
visited[source]=true;
while(!q.empty())
{
v = q.front();
q.pop();
for (i=0;i<graph[v].size();i++)
{
j = graph[v][i];
if (!visited[j] && cap[v][j] - flow[v][j]>0)
{
q.push(j);
dist[j] = dist[v]+1;
visited[j] = true;
}
}
}
if(visited[sink])
return true;
return false;
}
long long dinic(int v, int sink, long long flo)
{
if (v == sink || flo == 0)
return flo;
long long fl = 0;
int j;
for(;ptr[v]<graph[v].size();++ptr[v])
{
j = graph[v][ptr[v]];
if(cap[v][j] - flow[v][j] >0 && dist[j] == dist[v]+1)
{
long long f = dinic(j, sink, min(flo,cap[v][j] - flow[v][j]));
if(f == 0)
continue;
flow[v][j] += f;
flow[j][v] -= f;
flo -= f;
fl += f;
//return f;
}
}
dist[v] = INT_MAX;
return fl;
}
long long max_flow(int source, int sink)
{
long long flo = 0, f;
while(bfs(source, sink))
{
f = dinic(source,sink,LONG_LONG_MAX);
flo += f;
if(f == 0)
break;
}
return flo;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int m;
cin>>n>>m;
int u,v,w;
for(int i=0;i<m;i++)
{
cin>>u>>v>>w;
u--;
v--;
if (cap[u][v])
{
cap[u][v] += w;
cap[v][u] += w;
}
else
{
cap[u][v] += w;
cap[v][u] += w;
graph[v].push_back(u);
graph[u].push_back(v);
}
}
cout<<max_flow(0, n-1)<<endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp2ZWN0b3I8aW50PiBkaXN0Owp2ZWN0b3I8aW50PiBwdHI7CnZlY3RvcjwgdmVjdG9yPGludD4gPiBncmFwaCg1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