#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define SZ(a) int((a).size())
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int N=5e5+5;
const int M=8e6+5;
//il int Add(int &x,ll y) {return x=x+y>=mod?x+y-mod:x+y;}
//il int Mul(int &x,ll y) {return x=x*y>=mod?x*y%mod:x*y;}
struct Edge {
int to,nex,cap,flow;
} edge[M];
struct Dinic {
int n,s,t,cur[N],d[N],head[N],tol;
bool vis[N];
void init(int _n,int _s,int _t) {
n=_n,s=_s,t=_t,tol=0;
for(int i=0; i<n; ++i) head[i]=-1;
}
void add(int u,int v,int c) {
edge[tol].to=v,edge[tol].cap=c,edge[tol].flow=0;
edge[tol].nex=head[u],head[u]=tol++;
edge[tol].to=u,edge[tol].cap=0,edge[tol].flow=0;
edge[tol].nex=head[v],head[v]=tol++;
}
int que[N],front,tail;
bool bfs() {
for(int i=0; i<n; ++i) vis[i]=false;
front=0,tail=-1,que[++tail]=s,vis[s]=true,d[s]=0;
while(front<=tail) {
int x=que[front++];
for(int i=head[x]; ~i; i=edge[i].nex) {
Edge& e=edge[i];
if(!vis[e.to]&&e.cap>e.flow) {
vis[e.to]=true;
d[e.to]=d[x]+1;
que[++tail]=e.to;
}
}
}
return vis[t];
}
int dfs(int x,int a) {
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x]; ~i; i=edge[i].nex) {
Edge& e=edge[i];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0) {
e.flow+=f,edge[i^1].flow-=f;
flow+=f,a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow() {
int flow=0;
while(bfs()) {
for(int i=0; i<n; ++i) cur[i]=head[i];
flow+=dfs(s,inf);//流量上限为inf
}
return flow;
}
} dinic;
int n,m,s,t;
int main() {
std::ios::sync_with_stdio(0);
cin>>n>>m>>s>>t;
dinic.init(n+5,s,t); //初始化范围,源点 汇点
int a,b,w;
for(int i=0;i<m;++i){
cin>>a>>b>>w;
dinic.add(a,b,w);
}
cout<<dinic.Maxflow()<<endl;
return 0;
}