#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[110000];int len,last[110000],st,ed;
inline void ins(int x,int y,int c)
{
    len++;int k1,k2;k1=len;
    a[len].x=x,a[len].y=y,a[len].c=c;
    a[len].next=last[x],last[x]=len;
    len++;k2=len;
    a[len].x=y,a[len].y=x,a[len].c=0;
    a[len].next=last[y];last[y]=len;
    a[k1].other=k2;
    a[k2].other=k1;
}
int h[11000],list[11000];
inline bool bt_h()
{
    memset(h,0,sizeof(h));h[st]=1;
    list[1]=st;int head=1,tail=2;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(a[k].c>0&&h[y]==0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]>0)return true;
    else return false;
}
int findflow(int x,int f)
{
    if(x==ed)return f;
    int s=0,t;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(a[k].c>0&&s<f&&h[y]==h[x]+1)
        {
            s+=(t=findflow(y,min(a[k].c,f-s)));
            if(t==0)h[y]=0;
            a[k].c-=t,a[a[k].other].c+=t;
        }
    }
    return s;
}
int main()
{
    int n,m;
    while(scanf("%d%d%d%d",&n,&m,&st,&ed)!=EOF)
    {
        len=0,memset(last,0,sizeof(last));
        memset(list,0,sizeof(list));
        for(int i=1;i<=m;i++)
        {
            int x,y,c;scanf("%d%d%d",&x,&y,&c);
            ins(x,y,c);
        }
        int s=0;
        while(bt_h()==true)
        {
            s+=findflow(st,999999999);
        }
        printf("%d\n",s);
    }
    return 0;
}