#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=3e4+5;
const ll inf=1e18;
int tot=1,S,T,hd[N],nwhd[N],dep[N],nt[M],pt[M],vl[M],nwfl[M];
int n,m;
inline void addedge(int x,int y,int w){
++tot;nt[tot]=hd[x];pt[tot]=y;vl[tot]=w;hd[x]=tot;
}
inline bool bfs(){
queue<int> q;q.push(S);
memcpy(nwhd,hd,sizeof(hd));
for(int i=0;i<=n;++i)dep[i]=-1;
dep[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=hd[x];i;i=nt[i])
if(dep[pt[i]]<0&&vl[i]>nwfl[i]){
dep[pt[i]]=dep[x]+1;
q.push(pt[i]);
}
}
return dep[T]>=0;
}
inline ll dfs(int x,ll fl){
if(x==T)return fl;
if(!fl)return 0;
ll tfl=0;
for(int i=nwhd[x];i;i=nt[i]){
nwhd[x]=i;
if(dep[pt[i]]==dep[x]+1){
int pmfl=min(fl,(ll)vl[i]-nwfl[i]);
if(pmfl){
pmfl=dfs(pt[i],pmfl);
fl-=pmfl;tfl+=pmfl;
nwfl[i]+=pmfl;
nwfl[i^1]-=pmfl;
}
}
if(!fl)break;
}
if(!tfl)dep[x]=-1;
return tfl;
}
int main(){
ios::ysnc_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>S>>T;
for(int i=1,x,y,w;i<=m;++i)
cin>>x>>y>>w,addedge(x,y,w),addedge(y,x,0);
ll ans=0;
while(bfs())
ans+=dfs(S,inf);
cout<<ans<<'\n';
}