const nmax=1000;
var h,g:text;
ct,f:array[1..nmax,1..nmax]of longint;
t,viz,c:array[1..nmax]of longint;
flux,min,i,j,sursa,dest,m,n,x,y,z:longint;
function bf:boolean;
var i,pc,uc,x:longint;
begin
for i:=1 to n do
viz[i]:=0;
pc:=1;
uc:=1;
c[1]:=sursa;
viz[sursa]:=1;
while pc<=uc do
begin
x:=c[pc];
for i:=1 to n do
if (ct[x,i]-f[x,i]>0) and (viz[i]=0) then
begin
viz[i]:=1;
uc:=uc+1;
t[i]:=x;
c[uc]:=i;
end;
pc:=pc+1;
end;
if viz[dest]=0 then bf:=false
else bf:=true;
end;
begin
assign(h,'maxflow.in');
reset(h);
assign(g,'maxflow.out');
rewrite(g);
readln(h,n,m);
for i:=1 to m do
begin
readln(h,x,y,z);
ct[x,y]:=z;
end;
sursa:=1;
dest:=n;
while bf do
for i:=1 to n do
if ct[i,n]-f[i,n]>0 then
begin
min:=ct[i,n]-f[i,n];
j:=i;
while j<>sursa do
begin
if ct[t[j],j]-f[t[j],j]>0 then
min:=ct[t[j],j]-f[t[j],j];
j:=t[j];
end;
j:=i;
while j<>sursa do
begin
f[t[j],j]:=f[t[j],j]+min;
f[j,t[j]]:=f[j,t[j]]-min;
j:=t[j];
end;
f[i,n]:=f[i,n]+min;
f[n,i]:=f[n,i]-min;
flux:=flux+min;
end;
writeln(g,flux);
close(g);
end.