#include<bits/stdc++.h>
using namespace std;
long long Source,Collect;
long long N;
long long**cap;
long long**flow;
long long*Ceng_ISAP;
long long*cur;
long long*gap;
long long BFS_ISAP(){
for(long long i=0;i<N;i++){
Ceng_ISAP[i]=N;
}
Ceng_ISAP[Collect]=0;
queue<long long> que;
que.push(Collect);
while(!que.empty()){
long long temp=que.front();
que.pop();
gap[Ceng_ISAP[temp]]++;
for(long long i=0;i<N;i++){
if(Ceng_ISAP[i]==N&&cap[i][temp]-flow[i][temp]>0){
Ceng_ISAP[i]=Ceng_ISAP[temp]+1;
que.push(i);
}
}
}
return Ceng_ISAP[Source]<N;
}
long long DFS_ISAP(long long now_p,long long limit)
{
if(now_p==Collect) return limit;
long long cost=0;
for(long long i=cur[now_p];i<N;i++){
cur[now_p]=i;
if(Ceng_ISAP[now_p]==Ceng_ISAP[i]+1&&cap[now_p][i]-flow[now_p][i]>0){
long long temp=DFS_ISAP(i,min(limit-cost,cap[now_p][i]-flow[now_p][i]));
if(temp>0){
flow[now_p][i]+=temp;
flow[i][now_p]-=temp;
cost+=temp;
if(limit==cost) return cost;
}
}
}
gap[Ceng_ISAP[now_p]]--;
if(gap[Ceng_ISAP[now_p]]==0){
Ceng_ISAP[Source]=N;
}
long long minn=N-1;
for(long long i=0;i<N;i++){
if(cap[now_p][i]-flow[now_p][i]>0) minn=min(minn,Ceng_ISAP[i]);
}
Ceng_ISAP[now_p]=minn+1;
gap[Ceng_ISAP[now_p]]++;
cur[now_p]=0;
return cost;
}
long long ISAP()
{
long long ret=0;
Ceng_ISAP=new long long[N];
cur=new long long[N];
gap=new long long[N+1];
BFS_ISAP();
while(Ceng_ISAP[Source]<N){
for(long long i=0;i<N;i++){
cur[i]=0;
}
ret+=DFS_ISAP(Source,INT_MAX);
}
delete[]Ceng_ISAP;
delete[]cur;
delete[]gap;
return ret;
}
void CreatMapp();
void DeleteMemory();
int main ()
{
CreatMapp();
cout<<ISAP()<<endl;
DeleteMemory();
}
void CreatMapp(){
cin>>N;
long long m;
cin>>m;
cin>>Source;Source--;
cin>>Collect;Collect--;
cap=new long long*[N];
flow=new long long*[N];
for(long long i=0;i<N;i++){
cap[i]=new long long[N];
flow[i]=new long long[N];
for(long long j=0;j<N;j++){
cap[i][j]=flow[i][j]=0;
}
}
for(long long i=0;i<m;i++){
long long u,v,c;
cin>>u>>v>>c;
cap[u-1][v-1]=+c;
}
}
void DeleteMemory(){
for(long long i=0;i<N;i++){
delete[]cap[i];
delete[]flow[i];
}
delete[]cap;
delete[]flow;
}