#include<bits/stdc++.h>
using namespace std;
const int INF=0x7fffffff;
int n,m,s,t;
int a[10005][3],last[250],len;
int d[250];
void add(int x,int y,int z){
len++;
a[len][0]=x;
a[len][1]=last[y];
a[len][2]=z;
last[y]=len;
}
int bfs(){
memset(d,0,sizeof(d));
int i,j;
queue<int> q;
d[s]=1;
q.push(s);
while(!q.empty()){
int x=q.front();q.pop();
for(i=last[x];i;i=a[i][1]){
int y=a[i][0];
if(a[i][2]>0&&!d[y]){
d[y]=d[x]+1;
q.push(y);
}
}
}
return d[t];
}
int dfs(int s,int flow){
int i,j;
int tmp1=0,tmp2=flow;
if(s==t)return flow;
for(i=last[s];i;i=a[i][1]){
int y=a[i][0];
if(d[y]=d[s]+1&&a[i][2]>0){
int f=dfs(y,min(flow,a[i][2]));
tmp2-=f;
tmp1+=f;
a[i][2]-=f;
a[i^1][2]+=f;
}
if(tmp2<=0)break;
}
return tmp1;
}
int main(){
int i,j;
cin>>n>>m>>s>>t;
for(i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
add(y,x,z);
add(x,y,0);
}
long long ans=0;
while(bfs()){
ans+=dfs(s,INF);
}
cout<<ans;
return 0;
}