#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define N 1000005
typedef long long LL;
#define INF 1000000000000000000ll
struct mzls
{
int to,nt;
LL w;
}a[N];
int n,m,s,t,cnt,head[N],lev[N],iter[N];
inline void add(int x,int y,LL z)
{
a[cnt].to=y,a[cnt].w=z,a[cnt].nt=head[x],head[x]=cnt++;
a[cnt].to=x,a[cnt].w=0,a[cnt].nt=head[y],head[y]=cnt++;
}
inline void bfs(int s)
{
memset(lev,-1,sizeof(lev));
lev[s]=0;
queue<int>que;
que.push(s);
while(!que.empty())
{
int x=que.front();
que.pop();
for(int i=head[x];i!=-1;i=a[i].nt)
if(a[i].w>0&&lev[a[i].to]<0)
{
lev[a[i].to]=lev[x]+1;
que.push(a[i].to);
}
}
}
inline LL dfs(int x,int t,LL ml)
{
if(x==t)
return ml;
for(int &i=iter[x];i!=-1;i=a[i].nt)
{
int v=a[i].to;
LL w=a[i].w;
if(w<=0||lev[v]<=lev[x])
continue;
LL x1=dfs(v,t,min(ml,w));
if(x1>0)
{
a[i].w-=x1;
a[i^1].w+=x1;
return x1;
}
}
return 0;
}
inline LL max_flow(int s,int t)
{
LL ret=0;
while(1)
{
bfs(s);
if(lev[t]<0)
return ret;
memcpy(iter,head,sizeof(head));
LL x1;
while((x1=dfs(s,t,INF))>0)
ret+=x1;
}
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
printf("%lld",max_flow(s,t));
}