import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] nmst = br.readLine().split(" ");
int n = Integer.parseInt(nmst[0]);
int m = Integer.parseInt(nmst[1]);
int s = Integer.parseInt(nmst[2]);
int t = Integer.parseInt(nmst[3]);
Main edmondKarp = new Main();
edmondKarp.resolve(n, m, s, t, br);
}
public void resolve (int n, int m, int s, int t, BufferedReader br) throws IOException {
buildGraph(n, m, br);
pre = new int[100000];
minW = new long[100000];
long ans = 0;
while (bfs(s, t)) {
ans += minW[t];
modifyEdgeWeight(s, t);
}
System.out.println(ans);
}
public void modifyEdgeWeight(int source, int target) {
int cur = target;
while (cur != source) {
int preEdge = pre[cur];
edgeWeight[preEdge] -= minW[target];
edgeWeight[preEdge ^ 1] += minW[target];
cur = edgeTo[preEdge ^ 1];
}
}
public int[] pre;
public long[] minW;
public boolean bfs (int source, int target) {
Arrays.fill(pre, -2);
Queue<Integer> queue = new LinkedList<>();
pre[source] = -1;
queue.add(source);
minW[source] = Integer.MAX_VALUE;
while (!queue.isEmpty()) {
int curNode = queue.poll();
for (int e = head[curNode]; e != -1; e = nextEdge[e]) {
int nextNode = edgeTo[e];
if (pre[nextNode] == -2 && edgeWeight[e] > 0) {
minW[nextNode] = Math.min(minW[curNode], edgeWeight[e]);
queue.add(nextNode);
pre[nextNode] = e;
if (nextNode == target) {
return true;
}
}
}
}
return false;
}
public int[] head;
public int[] edgeTo;
public long[] edgeWeight;
public int[] nextEdge;
public int nEdge;
public int nNode;
public Map<String, Integer> duplicateEdgeHelper;
public void buildGraph(int n, int m, BufferedReader br) throws IOException {
head = new int[100000];
Arrays.fill(head, -1);
edgeTo = new int[200000];
edgeWeight = new long[200000];
nextEdge = new int[200000];
nNode = n;
duplicateEdgeHelper = new HashMap<>();
for (int i = 0; i < m; i ++) {
String[] inputs = br.readLine().split(" ");
int from = Integer.parseInt(inputs[0]);
int to = Integer.parseInt(inputs[1]);
int weight = Integer.parseInt(inputs[2]);
addEdge(from, to, weight);
addEdge(to, from, 0);
}
}
public void addEdge(int from, int to, int weight) {
if (duplicateEdgeHelper.containsKey(from + "," + to)) {
edgeWeight[duplicateEdgeHelper.get(from + "," + to)] += weight;
return ;
}
edgeTo[nEdge] = to;
edgeWeight[nEdge] = weight;
nextEdge[nEdge] = head[from];
head[from] = nEdge;
duplicateEdgeHelper.put(from + "," + to, nEdge ++);
}
}