#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=200005,maxm=20000005;
int n,m,s,t,head[maxn],cur[maxn],cnt=1,gap[maxm],ans,e[maxn],h[maxn],vis[maxn];
struct name{
int next,to,v;
}a[maxn];
struct cmp{
bool operator ()(int xi,int yi)const{return h[xi]<h[yi];}
};
void add(int x,int y,int z){
a[++cnt].v=z;
a[cnt].to=y;
a[cnt].next=head[x];
head[x]=cnt;
}
void bfs(){
for(int i=0;i<=n;i++) h[i]=1e16;
h[t]=0;
queue<int> q;
q.push(t);
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=head[x];i;i=a[i].next){
int y=a[i].to;
if(a[i^1].v&&h[y]>h[x]+1){
h[y]=h[x]+1;
q.push(y);
}
}
}
}
priority_queue<int,vector<int>,cmp> q;
void push(int x){
for(int i=head[x];i;i=a[i].next){
int y=a[i].to;
if(a[i].v&&h[y]+1==h[x]){
int mflow=min(a[i].v,e[x]);
a[i].v-=mflow;
a[i^1].v+=mflow;
e[x]-=mflow;
e[y]+=mflow;
if(!vis[y]&&y!=s&&y!=t){
q.push(y);
vis[y]=1;
}
}
if(!e[x]) break;
}
}
void rla(int x){
h[x]=1e16;
for(int i=head[x];i;i=a[i].next){
int y=a[i].to;
if(a[i].v&&h[y]+1<h[x]){
h[x]=h[y]+1;
}
}
}
void hlpp(){
h[s]=n;
for(int i=1;i<=n;i++){
if(h[i]!=h[0]) gap[h[i]]++;
}
for(int i=head[s];i;i=a[i].next){
int y=a[i].to,flow=a[i].v;
if(!flow) continue;
a[i].v-=flow;
a[i^1].v+=flow;
e[y]+=flow;
e[s]-=flow;
if(y!=t&&!vis[y]&&h[y]!=h[0]){
q.push(y);
vis[y]=1;
}
}
while(!q.empty()){
int x=q.top();
q.pop();
vis[x]=0;
push(x);
if(e[x]){
gap[h[x]]--;
if(!gap[h[x]]){
for(int i=1;i<=n;i++){
if(i!=s&&i!=t&&h[i]>h[x])
h[i]=n+1;
}
}
rla(x);
q.push(x);
gap[h[x]]++;
vis[x]=1;
}
}
}
signed main(){
cin>>n>>m>>s>>t;
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
add(y,x,0);
}
bfs();
if(h[t]==h[0]){
cout<<0;
return 0;
}
hlpp();
cout<<e[t];
return 0;
}