Program Ford_Fulkerson_Flux;
type matr =array[1..1000,1..1000] of longint;
var c,f :^matr;
n,i,m,j,t:longint;
fil :text; found :boolean;
seen :set of byte;
Function FindFlow(i,min:longint):longint;
var p,tmp:longint;
begin
seen:=seen+[i];
FindFlow:=min;
if i=n then found:=true
else
for p:=1 to n do
if (not (p in seen))and(not found) then begin
if (c^[i,p]-f^[i,p])>0 then begin
if c^[i,p]-f^[i,p]<min then min:=c^[i,p]-f^[i,p];
tmp:=FindFlow(p,min);
if found then begin
f^[i,p]:=f^[i,p]+tmp;
FindFlow:=tmp end
end;
if (f^[p,i]>0) and (not found) then begin
if f^[p,i]<min then min:=f^[p,i];
tmp:=FindFlow(p,min);
if found then begin
f^[p,i]:=f^[p,i]-tmp;
FindFlow:=tmp end
end
end
end;
begin
new(c); fillchar(c^,sizeof(c^),0);
new(f); fillchar(f^,sizeof(f^),0);
assign(fil,'maxflow.in');reset(fil);
readln(fil,n,m);
for t:=1 to m do readln(fil,i,j,c^[i,j]);close(fil);
repeat
found:=false;
seen:=[];
FindFlow(1,maxlongint)
until not found;
j:=0;
for i:=1 to n do
if c^[1,i]<>0 then j:=j+f^[1,i];
assign(fil,'maxflow.out');rewrite(fil);
writeln(fil,j);
close(fil);
end.