#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)
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;
const int maxn = 50000+50;
const int maxm = 200000+200;
#define Ci const int &
int n,m,s,t;
struct edge{
int u,v,n,flow;
I edge(Ci _u=0,Ci _v=0,Ci _n=0,Ci _flow=0):u(_u),v(_v),n(_n),flow(_flow){}
}e[maxm];
int cnt=1,head[maxn],cur[maxn];
I void add(Ci u,Ci v,Ci flow){
e[++cnt]=edge(u,v,head[u],flow),head[u]=cnt;
e[++cnt]=edge(v,u,head[v],0),head[v]=cnt;
}
int dep[maxn];
#include<queue>
I bool bfs(){
queue<int>q;
memset(dep,0,sizeof dep);
memcpy(cur,head,sizeof(head));
dep[s]=1,q.push(s);
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(e[ee].flow&&dep[v]==0)
dep[v]=dep[u]+1,q.push(v);
}
return dep[t];
}
int dfs(int u,int limit){
if(limit==0||u==t)return limit;
int flow=0,f;
for(int ee=cur[u],v;cur[u]=ee,v=e[ee].v,ee;ee=e[ee].n)
if(dep[v]==dep[u]+1&&(f=dfs(v,min(limit,e[ee].flow)))){
flow+=f;
limit-=f;
e[ee].flow-=f;
e[ee^1].flow+=f;
if(!limit)break;
}
return flow;
}
I int dinic(){
int ans=0;
while(bfs())
ans+=dfs(s,inf);
return ans;
}
signed main(){
read(n),read(m),read(s),read(t);
int u,v,f;
FOR(i,1,m)
read(u),read(v),read(f),
add(u,v,f);
write(dinic());
flush();
return 0;
}