#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 5;
const int INF = 1<<31 - 1;
int to[maxn],nxt[maxn], head[maxn], d[maxn], cur[maxn];
ll f[maxn];
int n, m, s, t, cnt = 1;
ll sum = 0;
void add_edge(int x, int y, int z)
{
to[++cnt] = y, nxt[cnt] = head[x], f[cnt] = z, head[x] = cnt;
}
bool bfs()
{
queue<int>q;
for(int i = 1; i <= n; ++i)cur[i] = head[i];
memset(d, -1, sizeof(d));
q.push(s), d[s] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = head[u]; i; i = nxt[i])
{
if(f[i] && d[to[i]] == -1)d[to[i]] = d[u] + 1, q.push(to[i]);
}
}
return d[t]!=-1;
}
int dfs(int u, ll flow)
{
ll cnt = 0;
if(u == t)return flow;
for(int i = cur[u]; i; i = nxt[i])
{
cur[u] = i;
int v = to[i];
if(d[v] == d[u] + 1 && f[i] > 0)
{
int df = dfs(v, min(flow, f[i]));
if(df) {cnt += df, f[i]-= df; f[i ^ 1] += df; flow -= df;}
if(!flow)break;
}
}
return cnt;
}
void dinic()
{
int tf = 0;
while(bfs())
{
sum += dfs(s, INF);
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
while(cin >> n >> m >> s >> t)
{
while(m--)
{
int x, y, z;
cin >> x >> y >> z;
add_edge(x, y, z), add_edge(y, x, 0);
}
dinic();
cout << sum << endl;
}
}