#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200000+10,M=1e4+5,mod=1e9+7;
ll n,m,s,t,h[N],cur[N],cnt=1,vis[N];
struct node{
ll to,nt,w;
}e[N];
void add(ll u,ll v,ll w)
{
e[++cnt]={v,h[u],w};
h[u]=cnt;
}
queue<ll>q;
bool bfs()
{
memset(vis,0,sizeof(vis));
vis[s]=1;
q.push(s);
while(!q.empty())
{
ll u=q.front();
q.pop();
cur[u]=h[u];
for(ll i=h[u];i;i=e[i].nt)
{
ll v=e[i].to,w=e[i].w;
if(w&&!vis[v])
{
vis[v]=vis[u]+1;
q.push(v);
}
}
}
return vis[t];
}
ll dfs(ll u,ll flow)
{
if(u==t)
return flow;
ll res=flow;
for(ll i=cur[u];i;i=e[i].nt)
{
ll v=e[i].to,w=e[i].w;
if(w&&vis[u]+1==vis[v])
{
ll now=dfs(v,min(res,w));
if(!now)
vis[v]=1;
else
{
e[i].w-=now;
e[i^1].w+=now;
res-=now;
}
}
if(!res)
return flow;
}
return flow-res;
}
int main()
{
cin>>n>>m>>s>>t;
for(int i=1;i<=m;i++)
{
ll u,v,w;
cin>>u>>v>>w;
add(u,v,w);
add(v,u,0);
}
ll ans=0;
while(bfs())
{
ans+=dfs(s,0x7fffffff);
}
cout<<ans<<endl;
return 0;
}