#include<bits/stdc++.h>
#define maxn 1005
using namespace std;
int n,m,res,s,t;
struct data
{
            int v,f,c;
};
vector <data> e;
vector<int>g[maxn];
int dd[maxn],cnt,d[maxn],pos[maxn];
bool bfs()
{
            queue<int> q;
            dd[s]=++cnt;
            while(!q.empty()) q.pop();
            q.push(s);
            while(!q.empty())
            {
                        int u=q.front();
                        pos[u]=0;
                        q.pop();
                        for (int i=0; i<(int)g[u].size(); ++i)
                        {
                                    int id=g[u][i];
                                    int v=e[id].v;
                                    if (e[id].f==e[id].c||dd[v]==cnt) continue;
                                    dd[v]=cnt;
                                    d[v]=d[u]+1;
                                    q.push(v);
                        }
            }
            return dd[t]==cnt;
}
int dfs(int u,int low)
{
            if (u==t||low==0) return low;
            for (; pos[u]<g[u].size(); ++pos[u])
            {
                        int id=g[u][pos[u]];
                        int v=e[id].v;
                        if (d[v]!=d[u]+1||e[id].f==e[id].c) continue;
                        int get=dfs(v,min(low,e[id].c-e[id].f));
                        if (get)
                        {
                                    e[id].f+=get;
                                    e[id^1].f-=get;
                                    return get;
                        }
            }
            return 0;
}
int main()
{
            //freopen("NKFLOW.inp","r",stdin);
            ios::sync_with_stdio(0);
            cin.tie(0);
            cout.tie(0);
            cin>>n>>m>>s>>t;
            for (int i=1; i<=m; ++i)
            {
                        int u,v,w;
                        cin>>u>>v>>w;
                        g[u].push_back(e.size());
                        data x;
                        x.v=v;
                        x.c=w;
                        x.f=0;
                        e.push_back(x);
                        g[v].push_back(e.size());
                        x.v=u;
                        x.c=0;
                        x.f=0;
                        e.push_back(x);
            }
            while(bfs())
            {
                        while(int x=dfs(s,10000000))
                        {
                                    res+=x;
                        }
            }
            cout<<res;
            return 0;
}
