N, M, inf = 1010, 20010, float('inf')
h, e, f, ne, idx = [-1] * N, [0] * M, [0] * M, [0] * M, 0
d, pre = [0] * N, [0] * N
def add(a, b, c):
global idx
e[idx], f[idx], ne[idx], h[a] = b, c, h[a], idx
idx += 1
def bfs():
q = [0] * N
q[0] = S
hh, tt = 0, 1
undo = [True] * N
undo[S] = False
d[S] = inf
while hh < tt:
u = q[hh]; hh += 1
i = h[u]
while i != -1:
v = e[i]
if undo[v] and f[i]:
undo[v] = False
d[v] = min(d[u], f[i])
pre[v] = i
if v == T:
return True
q[tt] = v; tt += 1
i = ne[i]
return False
def EK():
ans = 0
while bfs():
ans += d[T]
i = T
while i != S:
f[pre[i]] -= d[T]
f[pre[i] ^ 1] += d[T]
i = e[pre[i] ^ 1]
return ans
n, m, S, T = map(int, input().split())
while m:
a, b, c = map(int, input().split())
add(a, b, c); add(b, a, 0)
m -= 1
print(EK())