#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
const ll N=100010;
const ll M=200010;
ll h[N],ne[M],e[M],f[M],d[N],cur[M],idx,n,m,S,T;
const ll inf=LONG_LONG_MAX;
void add(ll u,ll v,ll w){
e[idx]=v,ne[idx]=h[u],f[idx]=w,h[u]=idx++;
e[idx]=u,ne[idx]=h[v],f[idx]=0,h[v]=idx++;
}
bool bfs(){
queue <ll> q;
q.push(S);
memset(d,-1,sizeof d);
cur[S]=h[S];
d[S]=0;
while(q.size()){
auto t=q.front();
q.pop();
for(int i=h[t];i!=-1;i=ne[i]){
int ver=e[i];
if(d[ver]==-1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if (ver == T) return true;
q.push(ver);
}
}
}
return false;
}
int find(ll u,ll limit){
if(u==T) return limit;
ll flow=0;
for(int i=cur[u];i!=-1 && flow<limit;i=ne[i]){
cur[u]=i;
int ver=e[i];
if(d[ver]==d[u]+1 && f[i]){
int t=find(ver,min(f[i],limit-flow));
if(t==0) d[ver]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
int main(){
cin>>n>>m>>S>>T;
memset(h,-1,sizeof h);
for(ll i=1;i<=m;i++){
ll u,v,w;
cin>>u>>v>>w;
add(u,v,w);
}
ll ans=0,flow;
while(bfs())while(flow = find(S,inf)) ans+=flow;
cout<<ans<<endl;
return 0;
}