1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <climits>
7 #include <vector>
8 #include <queue>
9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 2000;
18 struct arc{
19 int to,flow,next;
20 arc(int x = 0,int y = 0,int z = -1){
21 to = x;
22 flow = y;
23 next = z;
24 }
25 };
26 arc e[maxn<<3];
27 int head[maxn],d[maxn],cur[maxn];
28 int tot,S,T,n,m;
29 void add(int u,int v,int flow){
30 e[tot] = arc(v,flow,head[u]);
31 head[u] = tot++;
32 e[tot] = arc(u,0,head[v]);
33 head[v] = tot++;
34 }
35 bool bfs(){
36 queue<int>q;
37 memset(d,-1,sizeof(d));
38 d[T] = 1;
39 q.push(T);
40 while(!q.empty()){
41 int u = q.front();
42 q.pop();
43 for(int i = head[u]; ~i; i = e[i].next){
44 if(e[i^1].flow && d[e[i].to] == -1){
45 d[e[i].to] = d[u] + 1;
46 q.push(e[i].to);
47 }
48 }
49 }
50 return d[S] > -1;
51 }
52 int dfs(int u,int low){
53 if(u == T) return low;
54 int tmp = 0,a;
55 for(int &i = cur[u]; ~i; i = e[i].next){
56 if(e[i].flow && d[e[i].to]+1==d[u]&&(a=dfs(e[i].to,min(e[i].flow,low)))){
57 e[i].flow -= a;
58 e[i^1].flow += a;
59 low -= a;
60 tmp += a;
61 if(!tmp) break;
62 }
63 }
64 if(!tmp) d[u] = -1;
65 return tmp;
66 }
67 int dinic(){
68 int ans = 0;
69 while(bfs()){
70 memcpy(cur,head,sizeof(head));
71 ans += dfs(S,INF);
72 }
73 return ans;
74 }
75 int main() {
76 int u,v,w;
77 while(~scanf("%d %d",&n,&m)){
78 memset(head,-1,sizeof(head));
79 for(int i = tot = 0; i < m; ++i){
80 scanf("%d %d %d",&u,&v,&w);
81 add(u,v,w);
82 }
83 scanf("%d %d",&S,&T);
84 printf("%d\n",dinic());
85 }
86 return 0;
87 }