//这是用了STL的模板,分享一下
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
const int inf = 1e9;
int n, m, S, T;
struct Edge{
int from, to, cap, flow;
Edge(int u, int v, int c, int f):from(u),to(v),cap(c),flow(f) {}
};
vector<Edge> edges;//数量为边数的两倍
vector<int> G[maxn];//邻接表, G[i][j]表示结点i的第j条边在edges数组中的序号
int a[maxn];//s到每个节点i的路径上的最小残量 , a[i]总是正数也可以代替vis数组
int p[maxn];//增广路上的点的入弧编号
void init(int n) {
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void add(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(from, to, 0, 0));
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
int max_flow(int s, int t) {
int flow = 0;
for(;;)
{
fill(a, a+maxn, 0);
queue<int> q;
q.push(s);
a[s] = inf;
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 0; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if(!a[e.to] && e.cap > e.flow)//a[]可以代替bool数组
{
p[e.to] = G[x][i];//G[i][j]表示结点i的第j条边在edges数组中的序号
a[e.to] = min(a[x], e.cap - e.flow);
q.push(e.to);
}
}
if(a[t]) break;
}
if(!a[t]) break;
for(int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];// p[u]的反向边
}
flow += a[t];
}
return flow;
}
int main()
{
scanf("%d%d%d%d", &n, &m, &S, &T);
init(maxn);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
printf("%d\n", max_flow(S, T));
return 0;
}