// Sample implementation of the Edmonds Karp Algorithm
// Daniel Graf, grafdan@ethz.ch, 7.11.2015

#include <iostream>
#include <vector>
#include <cassert>
#include <queue>

#define INF 1000000000
typedef long long int in;
using namespace std;

struct Edge {
	in from, to, flow, cap, rev;
	in residual_capacity() {
		return cap-flow;
	}
};

struct Graph {
	in s, t;
	vector<vector<Edge> > E; // adjacency-list of edges
	vector<Edge*> P; // predecessor map for the BFS
	
	Graph(in N) {
		E = vector<vector<Edge> >(N);
	}

	void add_edge(in from, in to, in cap) {
		if(from == to) return;
		E[from].push_back({from,to,0,cap,(in)E[to].size()});
		E[to].push_back({to,from,0,0,(in)E[from].size()-1});
	}

	void reset_flow() {
		for(in v=0; v<E.size(); v++) {
			for(in e=0; e<E[v].size(); e++) {
				E[v][e].flow = 0;
			}
		}
	}
	
	bool find_flow() {
		P = vector<Edge*>(E.size(),NULL);
		// Breadth First Search through the edges with remaining capacity
		queue<in> Q;
		Q.push(s);
		while(!Q.empty() && P[t]==NULL) {
			in v = Q.front();
			Q.pop();
			for(in e=0; e<E[v].size(); e++) {
				if(E[v][e].residual_capacity()==0) {
					continue;
				}
				in w = E[v][e].to;
				if(P[w]==NULL) {
					P[w] = &(E[v][e]);
					Q.push(w);
				}
			}
		}
		// Check if there is a path to t
		if(P[t] == NULL) {
			return 0;
		}
		// Check the minimum capacity
		in flow = INF;
		in pos = t;
		while(pos != s) {
			flow = min(flow, P[pos]->residual_capacity());
			pos = P[pos]->from;
		}
		return flow;
	}

	void update_flow(in flow) {
		in pos = t;
		while(pos != s) {
			// cout << "update at vertex " << pos << endl;
			P[pos]->flow += flow;
			E[P[pos]->to][P[pos]->rev].flow -= flow;
			pos = P[pos]->from;
		}
	}

	in edmonds_karp_max_flow(in _s, in _t) {
		s = _s;
		t = _t;
		reset_flow();
		in flow = 0;
		in new_flow;
		do {
			new_flow = find_flow();
			flow += new_flow;
			if(new_flow > 0) {
				update_flow(new_flow);
			}
		} while(new_flow > 0);
		return flow;
	}
};

void read_graph_from_stdin(Graph &G) {
	in N,M;
	cin >> N >> M;
	G = Graph(N);
	for(in m=0; m<M; m++) {
		in a,b,c;
		cin >> a >> b >> c;
		G.add_edge(a,b,c);
	}
}

int main() {
	Graph G(0);
	read_graph_from_stdin(G);
	in s, t; cin >> s >> t;
	in res = G.edmonds_karp_max_flow(s,t);
	cout << "max flow: " << res << endl;
}