#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
int n, m, src, dst, depth[10001], ec;
struct EDGE
{
int to, weight;
EDGE() { }
EDGE(int t, int w) { to = t, weight = w; }
} edge[100001];
vector<int> g[10001];
void insert_edge(int s, int d, int w)
{
edge[ec] = EDGE(d, w);
g[s].push_back(ec++);
edge[ec] = EDGE(s, 0);
g[d].push_back(ec++);
}
bool bfs()
{
memset(depth, 0, sizeof(depth));
queue<int> q; EDGE e;
depth[src] = 1;
q.push(src);
do
{
int p = q.front();
q.pop();
for(int i = 0; i < g[p].size(); i++)
{
e = edge[g[p][i]];
if(e.weight > 0 && !depth[e.to])
{
depth[e.to] = depth[p] + 1;
q.push(e.to);
}
}
} while(!q.empty());
return depth[dst] > 0;
}
int dfs(int p, int cur)
{
if(p == dst) return cur;
EDGE e;
for(int i = 0; i < g[p].size(); i++)
{
e = edge[g[p][i]];
if(depth[e.to] == depth[p] + 1 && e.weight)
{
int c = dfs(e.to, min(cur, e.weight));
if(c > 0)
{
edge[g[p][i]].weight -= c;
edge[g[p][i] ^ 1].weight += c;
return c;
}
}
}
return 0;
}
int main()
{
int s, d, w;
scanf("%d%d%d%d", &n, &m, &src, &dst);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &s, &d, &w);
insert_edge(s, d, w);
}
int s = 0;
while(bfs())
s += dfs(src, 1e9 - 1);
printf("%d\n", s);
}