#include <iostream>
#include <cstdio>
#include <cstring>
#define REP(I, X) for(int I = 0; I < X; ++I)
#define FF(I, A, B) for(int I = A; I <= B; ++I)
#define clear(A, B) memset(A, B, sizeof A)
#define copy(A, B) memcpy(A, B, sizeof A)
#define min(A, B) ((A) < (B) ? (A) : (B))
#define max(A, B) ((A) > (B) ? (A) : (B))
using namespace std;
typedef long long ll;
typedef long long LL;
const int oo = 0x3f3f3f3f;
const int maxE = 1100050;
const int maxN = 10005;
const int maxQ = 1100050;
struct Edge{
int v, n;
long long c;
};
Edge edge[maxE];
int adj[maxN], cntE;
int Q[maxQ], head, tail, inq[maxN];
int d[maxN], num[maxN], cur[maxN], pre[maxN];
int s, t, nv;
int n, m, nm;
int path[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void addedge(int u, int v, long long c){
edge[cntE].v = v;edge[cntE].c = c; edge[cntE].n = adj[u]; adj[u] = cntE++;
edge[cntE].v = u;edge[cntE].c = 0; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
void REV_BFS(){
clear(d, -1);
clear(num, 0);
head = tail = 0;
d[t] = 0;
num[0] = 1;
Q[tail++] = t;
while(head != tail){
int u = Q[head++];
for(int i = adj[u]; ~i; i = edge[i].n){
int v = edge[i].v;
if(~d[v]) continue;
d[v] = d[u] + 1;
num[d[v]]++;
Q[tail++] = v;
}
}
}
long long ISAP(){
copy(cur, adj);
REV_BFS();
int u = pre[s] = s, i;
long long flow=0;
while(d[s] < nv){
if(u == t){
long long f = oo, neck;
for(i = s; i != t; i = edge[cur[i]].v){
if(f > edge[cur[i]].c){
f = edge[cur[i]].c;
neck = i;
}
}
for(i = s; i != t; i = edge[cur[i]].v){
edge[cur[i]].c -= f;
edge[cur[i] ^ 1].c += f;
}
flow += f;
u = neck;
}
for(i = cur[u]; ~i; i = edge[i].n) if(edge[i].c && d[u] == d[edge[i].v] + 1) break;
if(~i){
cur[u] = i;
pre[edge[i].v] = u;
u = edge[i].v;
}
else{
if(0 == (--num[d[u]])) break;
int mind = nv;
for(i = adj[u]; ~i; i = edge[i].n){
if(edge[i].c && mind > d[edge[i].v]){
mind = d[edge[i].v];
cur[u] = i;
}
}
d[u] = mind + 1;
num[d[u]]++;
u = pre[u];
}
}
return flow;
}
int read () {
char c = ' ' ;
int x = 0 ;
while ( c < '0' || c > '9' )
c = getchar () ;
while ( c >= '0' && c <= '9' ) {
x = x * 10 + c - '0' ;
c = getchar () ;
}
return x ;
}
void work()
{
cntE=0;
clear(adj, -1);
int u,v,w;
int sum=0;
s = 1 , t = n, nv = t + 1 ;
for(int i=1;i<=m;i++){
u = read () ;v = read () ;w = read () ;
sum+=u;
addedge ( u , v , w ) ;
}
LL flow =ISAP();
printf ( "%lld\n",flow) ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
work();
}
return 0;
}