#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#define INF 20000000000
using namespace std;
vector<int>L[500];
queue<int>q;
int n,cost,m,s,dr,a,b,ab,cc,i,j,vmin,ih,c[500][500],w[500][500],x[500][500],v[500],d[500],e[500],t[500],h[500],P[500],r[500],dh,poz;
FILE *f,*g;
int minim(int a,int b){
if(a<b)
return a;
return b;
}
void bf(){
q.push(s);
v[s]=1;
for(int i=1;i<=n;i++){
d[i]=INF;
}
d[s]=0;
while(!q.empty()){
a=q.front();
q.pop();
v[a]=0;
for(int i=0;i<L[a].size();i++){
if(d[ L[a][i] ]>d[a]+x[a][ L[a][i] ] &&c[a][ L[a][i] ] > w[a][ L[a][i] ]){
d[ L[a][i] ]=d[a]+x[a][ L[a][i] ] ;
if(v[ L[a][i] ]==0){
v[ L[a][i] ]=1;
q.push(L[a][i]);
}
}
}
}
}
void chg(int &a,int &b){
int aux=a;
a=b;
b=aux;
}
void del(){
h[1]=h[dh];
dh--;
P[h[1]]=1;
int p=1;
int c=2;
while(c<=dh){
if(c<dh&&e[ h[c] ]>e[ h[c+1] ])
c++;
if(e[h[c]]<e[h[p]]){
chg(h[p],h[c]);
P[ h[p] ]=p;
P[ h[c] ]=c;
p=c;
c*=2;
}
else
break;
}
}
void upd(int poz){
int c=poz;
int p=poz/2;
while(p>=1){
if(e[h[c]]<e[h[p]]){
chg(h[p],h[c]);
P[ h[p] ]=p;
P[ h[c] ]=c;
c=p;
p/=2;
}
else
break;
}
}
int dijkstra(){
memset(t,0,sizeof(t));
for(i=1;i<=n;i++){
e[i]=INF;
}
e[s]=r[s]=0;
h[1]=s;
dh=1;
P[s]=1;
int ok=0;
while(dh){
poz=h[1];
del();
for(i=0;i<L[poz].size();i++){
a=L[poz][i];
if(e[a]>e[poz]+x[poz][a]-d[a]+d[poz]&&c[poz][a]>w[poz][a]){
t[a]=poz;
if(a==dr)
ok=1;
if(e[a]==INF)
ih=0;
else
ih=1;
e[a]=e[poz]-d[a]+d[poz]+x[poz][a];
r[a]=r[poz]+x[poz][a];
if(ih==0){
h[++dh]=a;
P[ h[dh] ]=dh;
upd(dh);
}
else
upd(P[a]);
}
}
}
return ok;
}
int main(){
f=fopen("fmcm.in","r");
g=fopen("fmcm.out","w");
fscanf(f,"%d%d%d%d",&n,&m,&s,&dr);
for(i=1;i<=m;i++){
fscanf(f,"%d%d%d%d",&a,&b,&ab,&cc);
L[a].push_back(b);
L[b].push_back(a);
c[a][b]=ab;
x[a][b]=cc;
x[b][a]=-cc;
}
bf();
while(dijkstra()){
vmin=INF;
a=dr;
do{
vmin=minim(vmin,c[ t[a] ][a]-w[ t[a] ][a]);
a=t[a];
}while(a!=s);
if(vmin){
a=dr;
do{
w[ t[a] ][a]+=vmin;
w[a][ t[a] ]-=vmin;
a=t[a];
}while(a!=s);
}
cost+=(vmin*r[dr]);
}
fprintf(g,"%d",cost);
fclose(f);
fclose(g);
return 0;
}