#include<bits/stdc++.h>
using namespace std;
const int maxn=10005;
const int maxm=100005;
const int INF=0x7f7f7f;
int n,m,s,t,cnt=-1;
int head[maxn],dep[maxn];
struct node{
int to,cc,nex;
}edge[maxm*2];
void add_edge(int a,int b,int c)
{
edge[++cnt].nex=head[a];
edge[cnt].to=b;
edge[cnt].cc=c;
head[a]=cnt;
}
void add(int a,int b,int c)
{
add_edge(a,b, c);
add_edge(b,a,0);
}
int dfs(int u,int strm)
{
cout<<"dfs"<<" u:"<<u<<" strm:"<<strm<<endl;
if(u==t) return strm;
for(int i=head[u];i!=-1;i=edge[i].nex)
{
if(edge[i].cc && dep[edge[i].to]==dep[u]+1)
{
int d=dfs(edge[i].to,min(strm,edge[i].cc));
if(d>0)
{
edge[i].cc-=d;
edge[i^1].cc+=d;
return d;
}
}
}
return 0;
}
bool bfs()
{
cout<<"bfs"<<endl;
queue<int>q;
while(!q.empty())
q.pop();
memset(dep,-1,sizeof(dep));
dep[s]=0;
q.push(s);
do
{
int u=q.front();
cout<<"u:"<<u<<endl;
q.pop();
for(int i=head[u];i!=-1;i=edge[i].nex)
{
cout<<"v:"<<edge[i].to<<endl;
if(edge[i].cc>0 && dep[edge[i].to]==-1)
{
dep[edge[i].to]=dep[u]+1;
q.push(edge[i].to);
}
}
}
while(!q.empty());
if(dep[t]==-1) return false;
return true;
}
int dinic()
{
int ans=0;
while(bfs())
{
while(int d=dfs(s,INF))
ans+=d;
}
return ans;
}
int main()
{
freopen("input.txt","r",stdin);
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&m,&s,&t);
for(int i=1,u,v,w;i<=m;++i)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%d",dinic());
return 0;
}
using namespace std;
const int maxn=10005;
const int maxm=100005;
const int INF=0x7f7f7f;
int n,m,s,t,cnt=-1;
int head[maxn],dep[maxn];
struct node{
int to,cc,nex;
}edge[maxm*2];
void add_edge(int a,int b,int c)
{
edge[++cnt].nex=head[a];
edge[cnt].to=b;
edge[cnt].cc=c;
head[a]=cnt;
}
void add(int a,int b,int c)
{
add_edge(a,b, c);
add_edge(b,a,0);
}
int dfs(int u,int strm)
{
cout<<"dfs"<<" u:"<<u<<" strm:"<<strm<<endl;
if(u==t) return strm;
for(int i=head[u];i!=-1;i=edge[i].nex)
{
if(edge[i].cc && dep[edge[i].to]==dep[u]+1)
{
int d=dfs(edge[i].to,min(strm,edge[i].cc));
if(d>0)
{
edge[i].cc-=d;
edge[i^1].cc+=d;
return d;
}
}
}
return 0;
}
bool bfs()
{
cout<<"bfs"<<endl;
queue<int>q;
while(!q.empty())
q.pop();
memset(dep,-1,sizeof(dep));
dep[s]=0;
q.push(s);
do
{
int u=q.front();
cout<<"u:"<<u<<endl;
q.pop();
for(int i=head[u];i!=-1;i=edge[i].nex)
{
cout<<"v:"<<edge[i].to<<endl;
if(edge[i].cc>0 && dep[edge[i].to]==-1)
{
dep[edge[i].to]=dep[u]+1;
q.push(edge[i].to);
}
}
}
while(!q.empty());
if(dep[t]==-1) return false;
return true;
}
int dinic()
{
int ans=0;
while(bfs())
{
while(int d=dfs(s,INF))
ans+=d;
}
return ans;
}
int main()
{
freopen("input.txt","r",stdin);
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&m,&s,&t);
for(int i=1,u,v,w;i<=m;++i)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%d",dinic());
return 0;
}