#include <bits/stdc++.h>
//maybeeee we'll meet at a baaaar, we'll drivee a funky hhhhhhuuugh
using namespace std;
ifstream fin ("fmcm.in");
ofstream fout ("fmcm.out");
int n, m, s, D, maxflux, cost;
struct much{
int a, b, c, z, f, per;
};
much a[24005];
vector <int> v[355];
int d[355], dvechi[355], reald[355], last[355];
void omul_cu_clopot(){
for(int i = 1; i <= n; i++){
reald[i] = INT_MAX;
}
reald[s] = 0;
for(int i = 1; i < n; i++){
for(int j = 1; j <= 2 * m; j++){
reald[a[j].b] = min(reald[a[j].b], reald[a[j].a] + a[j].z);
}
}
}
void deschistra(){
priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair <int, int> > > q;
for(int i = 1; i <= n; i++){
dvechi[i] = reald[i];
d[i] = INT_MAX;
last[i] = -1;
}
d[s] = 0;
reald[s] = 0;
last[s] = 0;
q.push({0, s});
int nod, dnou;
much nxt;
while(!q.empty()){
dnou = q.top().first;
nod = q.top().second;
q.pop();
if(dnou != d[nod]){
continue;
}
for(int i = 0; i < v[nod].size(); i++){
nxt = a[v[nod][i]];
if(dnou + nxt.z + dvechi[nxt.a] - dvechi[nxt.b] < d[nxt.b] &&
nxt.f < nxt.c && nod != D){
last[nxt.b] = v[nod][i];
d[nxt.b] = dnou + nxt.z + dvechi[nxt.a] - dvechi[nxt.b];
reald[nxt.b] = reald[nod] + nxt.z;
q.push({d[nxt.b], nxt.b});
}
}
}
}
void fa_un_drum(){
int nod = D, minim_ude = INT_MAX;
while(last[nod] != 0){
minim_ude = min(minim_ude, a[last[nod]].c - a[last[nod]].f);
nod = a[last[nod]].a;
}
maxflux += minim_ude;
cost += minim_ude * reald[D];
nod = D;
while(last[nod] != 0){
a[last[nod]].f += minim_ude;
a[a[last[nod]].per].f -= minim_ude;
nod = a[last[nod]].a;
}
}
void flux(){
int lastflux = -1;
while(lastflux != maxflux){
lastflux = maxflux;
deschistra();
if(last[D] != -1){
fa_un_drum();
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fin >> n >> m >> s >> D;
for(int i = 1; i <= m; i++){
fin >> a[i].a >> a[i].b >> a[i].c >> a[i].z;
}
for(int i = 1; i <= m; i++){
a[i].per = i + m;
a[i+m] = {a[i].b, a[i].a, 0, -a[i].z, i};
v[a[i].a].push_back(i);
v[a[i].b].push_back(i + m);
}
omul_cu_clopot();
flux();
fout << cost;
return 0;
}