#include<bits/stdc++.h>
using namespace std;
int const NMax = 1005, oo = 1000000;
vector <int> G[NMax];
queue <int> q;
int pred[NMax], D[NMax], F[NMax][NMax];
bool M[NMax][NMax];
int m, n, source, target, t;
void Read()
{
scanf("%d %d", &n, &m);
int x, y, c;
source = 1;
target = n;
for(int i = 1; i<=m; ++i){
scanf("%d %d %d", &x, &y, &c);
G[x].push_back(y);
F[x][y] = c;
G[y].push_back(x);
}
}
bool Bell()
{
int i, node;
q.push(source);
for(i = 0; i <= target; i++){
D[i] = oo;
pred[i] = 0;
}
D[source] = 0;
while(q.size()){
node = q.front();
q.pop();
for(auto it : G[node])
if(D[it] == oo and F[node][it] > 0){
D[it] = D[node] + 1;
pred[it] = node;
q.push(it);
}
}
return D[target] != oo;
}
void Flow()
{
int flow, ind, node, maxflow = 0;
while(Bell()){
for(auto it : G[target]){
pred[target] = it;
flow = oo;
if(D[it] > D[target])
continue;
node = target;
while(0 < pred[node]){
flow = min(flow, F[pred[node]][node]);
node = pred[node];
}
if(flow == 0)
continue;
maxflow += flow;
node = target;
while(pred[node] > 0){
F[pred[node]][node] -= flow;
F[node][pred[node]] += flow;
node = pred[node];
}
}
}
printf("%d\n", maxflow);
}
void CleanUp()
{
int i, j;
for(i = 0; i <= target; i++){
G[i].clear();
memset(F[i], 0, sizeof(F[i]));
memset(M[i], 0, sizeof(M[i]));
}
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
Read();
Flow();
CleanUp();
return 0;
}