#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
using namespace std;
#define M 9499999
int c[10004][10004],f[10004][10004];
int a[1000],pre[1000],n,m,s,t;
int bfs(int s,int t)
{
int flow=0;
queue<int>q;
while(1){
memset(a,0,sizeof(a));
a[s]=M;
q.push(s);
while(!q.empty())
{
int u=q.front();q.pop();
for(int v=1;v<=m;v++)
if(!a[v]&&f[u][v]<c[u][v])
{
q.push(v);
a[v]=min(c[u][v]-f[u][v],a[u]);
pre[v]=u;
}
if(a[t]) break;
}
if(!a[t]) return flow;
for(int u=t;u!=s;u=pre[u])
{
f[u][pre[u]]-=a[t];
f[pre[u]][u]+=a[t];
}
flow+=a[t];
}
return flow;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1,u,v,w;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
c[u][v]+=w;
}
cout<<bfs(s,t);
return 0;
}