#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ReadNr(T &_value) {
bool _sign = 0;
char ch;
_value = 0;
while(!isdigit(ch = getchar())) {
if (ch == -1) return false;
_sign = (ch == '-');
}
do { _value = _value * 10 + (ch - '0');
} while(isdigit(ch = getchar()));
_value = _sign ? -_value : _value;
return true;
}
typedef struct lnod {
int nod,cost,cap,flow,last;
}nod;
const int INF=1e9+69*69;
int i,n,m,flow,rs,start,fin,x,y,cap,cost;
int q[355],tata[355],d[355],id[355],qh,qt,poz[355];
vector<nod> g[355];
void add(int x,int y,int cap,int cost) {
nod r1={y,cost,cap,0,(int)g[y].size()};
nod r2={x,-cost,0,0,(int)g[x].size()};
g[x].push_back(r1);
g[y].push_back(r2);
}
int Update() {
int addflow=INF;
for(x=fin;x!=start;x=tata[x])
{
int p=tata[x],pos=poz[x];
addflow=min(addflow,g[p][pos].cap-g[p][pos].flow);
}
for(x=fin;x!=start;x=tata[x])
{
int p=tata[x],pos=poz[x];
int rev=g[p][pos].last;
g[p][pos].flow+=addflow;
g[x][rev].flow-=addflow;
rs+=g[p][pos].cost*addflow;
}
return addflow;
}
int main()
{
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
ReadNr(n); ReadNr(m); ReadNr(start); ReadNr(fin);
for(; m; --m) {
ReadNr(x); ReadNr(y); ReadNr(cap); ReadNr(cost);
add(x,y,cap,cost);
}
while(1) {
for(i=0;i<=n+1;++i) id[i]=0,d[i]=INF;
qh=qt=0; q[qt++]=start; d[start]=0;
while(qh!=qt)
{
x=q[qh++]; id[x]=2;
if(qh==n) qh=0;
for(i=0;i<(int)g[x].size();++i)
{
nod &r=g[x][i];
if(r.flow<r.cap && d[r.nod]>d[x]+r.cost)
{
d[r.nod]=d[x]+r.cost;
if(!id[r.nod])
{
q[qt++]=r.nod;
if(qt==n) qt=0;
}
else if(id[r.nod]==2)
{
if(--qh==-1) qh=n-1;
q[qh]=r.nod;
}
id[r.nod]=1; tata[r.nod]=x; poz[r.nod]=i;
}
}
}
if(d[fin]==INF) break;
flow+=Update();
}
printf("%d\n", rs);
return 0;
}