#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 2000; 
int n, m, s, t, flow[maxn][maxn], capacity[maxn][maxn];
vector<int> Gin[maxn], Gout[maxn];
bool vis[maxn];

bool dfs(int u){ // agar az rawse u be rawse t masir afzayeshi peyda shavad true mishavad va masir ra 1 vahed ezafe mikonad
    vis[u] = true;
    if(u==t) return true;
    for(int i=0; i<Gout[u].size(); i++){
        int v= Gout[u][i];
        if(vis[v]==false && capacity[u][v]-flow[u][v]>0 && dfs(v)){
            flow[u][v] ++;
            return true;
            /*
            tarjome : agar rawse v dide nashode bood va ghabeliate afzayeshi shodan dasht
            va dfs(v) true shod(yawni az v be t masir vojood dasht) tebghe esteghra 
            masir yek vahed ziad mishavad pas kafi ast yale fewli ra yek vahed ziad konim
            ta hokme esteghra ra bar gharar sazim va sepas return konim
            */
        }
    }
    for(int i=0; i<Gin[u].size(); i++){
        int v=Gin[u][i];
        if(vis[v]==false && flow[u][v]>0 && dfs(v)){
            flow[u][v] --;
            return true;
            //in ghesmate bawdie kar mibashad ke yal haye bargashti tey mishavad
        }
    }
    return false; // agar hich masiri peyda nashod false barmigardanim
}

/*
nokte amoozeshi : 
if(a && b && c) -> be tartib a va b va c ra check mikonad va agar har kodam az anha ghalat bashad
baghie ra check nemikonad
hamintor if(a||b||c) be avvalin dorost beresad baghie ra check nemikonad
*/

int main(){
    cin >> n >> m >> s >> t;
    s--,t--; //convert to 0 base
    for(int i=0; i<m; i++){
        int u,v,c;
        cin >> u >> v >> c;
        u--,v--;
        capacity[u][v] = c;
        Gout[u].push_back(v);
        Gin[v].push_back(u);
    }
    
    while(dfs(s)){
        fill(vis, vis+n, false);
    }
    
    int flow_size=0;
    for(int i=0; i<n; i++)
        flow_size += flow[s][i];
    cout << flow_size << endl;
    
    return 0;
}