const nmax=1024;
var g,h:text;
j,min,flux,x,y,i,n,m,z,sursa,dest:integer;
viz,t,c:array[1..nmax] of integer;
ct,f:array[1..nmax,1..nmax]of integer;
function bf:boolean;
var p,i,u,x:integer;
begin
for i:=1 to n do viz[i]:=0;
p:=1;
u:=1;
c[1]:=sursa;
while p<=u do
begin
x:=c[p];
for i:=1 to n do
if (ct[x,i]-f[x,i]>0) and (viz[i]=0)then
begin
u:=u+1;
c[u]:=i;
viz[i]:=1;
t[i]:=x;
end;
p:=p+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<>1) do begin
if (ct[t[j],j]-f[t[j],j])<min then
min:=ct[t[j],j]-f[t[j],j];
j:=t[j];
end;
j:=i;
while (j<>1) 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.