#include<cstdio>
#define N 351
int c[N][N],n,m,t[N],i,j,k,l,b[N],s,d,a[N],g[N][N],o[N][N],r[N];
int A()
{int p,q,i,k,j;
for(i=1;i<=n;i++)
b[i]=N,t[i]=0;
for(r[0]=s,p=q=b[s]=0;p<=q&&!t[d];p++)
for(k=0;k<a[r[p]];k++)
{i=r[p],j=g[i][k];
if(c[i][j]&&b[j]>b[i]+o[i][j])
r[++q]=j,t[j]=i,b[j]=b[i]+o[i][j];}
return t[d]!=0;}
void B()
{int e=0,j,k,m;
while(A())
{for(k=0;k<a[d];k++)
if(t[g[d][k]])
{for(m=10001,j=g[d][k];t[j];j=t[j])
if(m>c[t[j]][j])
m=c[t[j]][j];
if(m>c[g[d][k]][d])
m=c[g[d][k]][d];
if(m<=0)
continue;
c[g[d][k]][d]-=m,c[d][g[d][k]]+=m;
for(j=g[d][k];t[j];j=t[j])
c[t[j]][j]-=m,c[j][t[j]]+=m;
e=e+m*b[d];}}
printf("%d\n",e);}
int main()
{freopen("fmcm.in","r",stdin);
freopen("fmcm.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&s,&d);
while(m--)
scanf("%d%d%d%d",&i,&j,&l,&k),g[i][a[i]++]=j,g[j][a[j]++]=i,o[i][j]=k,o[j][i]=-k,c[i][j]=l;
B();
return 0;}