#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,s,t,home[210],cnt=1,dis[210],now[210],ans;
struct node{
	ll next,to,w;
}e[10010];
bool bfs(){
	for(ll i=1;i<=n;i++) dis[i]=0x3f3f3f3f3f3f3f;
	queue<ll> q;
	q.push(s);
	dis[s]=0;
	now[s]=home[s];
	bool flag=false;
	while(!q.empty()){
		ll h=q.front();
		q.pop();
		for(ll i=home[h];i;i=e[i].next){
			ll to=e[i].to;
			if(dis[to]==0x3f3f3f3f3f3f3f&&e[i].w>0){
				dis[to]=dis[h]+1;
				q.push(to);
				now[to]=home[to];
				flag=flag||(to==t);
			}
		}
	}
	return flag;
}
ll dfs(ll h,ll maxn){
	if(h==t) return maxn;
	ll ret=0;
	for(ll i=now[h];i;i=e[i].next){
		now[h]=i;
		ll to=e[i].to;
		if(dis[to]==dis[h]+1&&e[i].w>0){
			ll k=dfs(to,min(e[i].w,maxn));
			if(k==0){
				dis[to]=0x3f3f3f3f3f3f3f;
				continue;
			}
			e[i].w-=k;
			e[i^1].w+=k;
			ret+=k;
			maxn-=k;
		}
	}
	return ret;
}
int main(){
	scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
	for(ll i=1;i<=m;i++){
		ll st,ed,fix;
		scanf("%lld%lld%lld",&st,&ed,&fix);
		e[++cnt].to=ed;
		e[cnt].next=home[st];
		e[cnt].w=fix;
		home[st]=cnt;
		e[++cnt].to=st;
		e[cnt].next=home[ed];
		e[cnt].w=0;
		home[ed]=cnt;
	}
	while(bfs()) ans+=dfs(s,0x3f3f3f3f3f3f3f);
	printf("%lld",ans);
	return 0;
}