#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cmath>
using namespace std;
#define I inline
#define R register
#define inf 1073742823
#define FOR(i,begin,end) for(R int i=begin;i<=end;++i)
#define ROF(i,begin,end) for(R int i=begin;i>=end;--i)
#include<queue>
namespace IO{
char buf[1<<21],*pa=buf,*pb=buf;
char buffer[1<<21];int p1=-1;const int p2=(1<<21)-1;
I char gc(){return pa==pb&&(pb=(pa=buf)+fread(buf,1,1<<21,stdin),pa==pb)?EOF:*pa++;}
template<class T>I void read(T &x){
x=0;R int y=0;R char ch=gc();
for(;!isdigit(ch);ch=gc())y=ch=='-';
for(;isdigit(ch);ch=gc())x=x*10+(ch^48);
(y)&&(x=-x);return;}
I void flush(){fwrite(buffer,1,p1+1,stdout);p1=-1;return;}
I void pc(int ch){if(p1==p2)flush();buffer[++p1]=ch;return;}
template<class T>I void write(T x){
static char buf[20];static int len =-1;
if(x>=0){do buf[++len]=(x%10)^48,x/=10;while(x);}
else{pc('-');do buf[++len]=(-(x%10))^48,x/=10;while(x);}
while(len>=0)pc(buf[len--]);
return;}
}
using namespace IO;
int n,m,s,t;
const int maxn = 50000+50;
const int maxm = 200000+200;
#define ci const int &
struct edge{
int u,v,f,n;
I edge(ci uu =0,ci vv=0,ci ff=0,ci nn=0):u(uu),v(vv),f(ff),n(nn){}
}e[maxm];
int cnt=1,head[maxn],cur[maxn],gap[maxn],dis[maxn],mxflow=0;
I void add(ci u,ci v,ci f){
e[++cnt]=edge(u,v,f,head[u]),head[u]=cnt;
e[++cnt]=edge(v,u,0,head[v]),head[v]=cnt;
}
I void bfs(){
queue<int>q;q.push(t),gap[dis[t]=1]++;
while(!q.empty()){
int u=q.front();q.pop();
for(int ee=head[u],v;v=e[ee].v,ee;ee=e[ee].n)
if(!dis[v])gap[dis[v]=dis[u]+1]++,q.push(v);
}
}
int dfs(ci u,ci flow){
if(u==t&&((mxflow+=flow)||1))return flow;
int us=0,f;
for(int ee=cur[u],v;cur[u]=ee,v=e[ee].v,ee;ee=e[ee].n)
if(dis[v]==dis[u]-1){
if(f=dfs(v,min(flow-us,e[ee].f)))
e[ee].f-=f,e[ee^1].f+=f,us+=f;
if(us==flow)return flow;
}
(--gap[dis[u]])?(++gap[++dis[u]]):dis[s]=n+1;
return us;
}
signed main(){
int u,v,f;
read(n),read(m),read(s),read(t);
FOR(i,1,m)read(u),read(v),read(f),add(u,v,f);
bfs();
while(dis[s]<=n+1)memcpy(cur,head,sizeof head),dfs(s,inf);
write(mxflow);
flush();
return 0;
}