#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 350;
const int MMAX = 12500;
const int INF = 1000000000;
struct muchie {
int a, b;
int cf, cost;
muchie(int ta = 0, int tb = 0, int tcf = 0, int tcost = 0) : a(ta), b(tb), cf(tcf), cost(tcost) {}
};
struct elem {
int nod, cost;
elem(int tnod = 0, int tcost = 0) : nod(tnod), cost(tcost) {}
};
bool operator > (elem e1, elem e2) {
return e1.cost > e2.cost;
}
int n, m, s, d, ctot;
muchie muchii[2 * MMAX + 5];
vector<int> v[NMAX + 5];
int bfd[NMAX + 5];
queue<int> q;
bool inq[NMAX + 5];
int from[NMAX + 5], mind[NMAX + 5], new_bfd[NMAX + 5];
priority_queue<elem, vector<elem>, greater<elem>> pq;
void bellman_ford(int start) {
for(int i = 1; i <= n; i++)
bfd[i] = INF;
bfd[start] = 0;
q.push(start);
inq[start] = true;
while(!q.empty()) {
int crt = q.front();
q.pop();
inq[crt] = false;
for(int vec: v[crt]) {
muchie &mvec = muchii[vec];
if(mvec.cf == 0 || bfd[crt] + mvec.cost >= bfd[mvec.b])
continue;
bfd[mvec.b] = bfd[crt] + mvec.cost;
if(!inq[mvec.b]) {
q.push(mvec.b);
inq[mvec.b] = true;
}
}
}
}
bool dijkstra(int start) {
bool vizd = false;
while(!pq.empty())
pq.pop();
for(int i = 1; i <= n; i++)
mind[i] = INF;
mind[start] = 0;
new_bfd[start] = 0;
from[start] = -1;
pq.push(elem(start, 0));
while(!pq.empty()) {
elem crt = pq.top();
pq.pop();
if(crt.cost > mind[crt.nod])
continue;
if(crt.nod == d) {
vizd = true;
continue;
}
for(int vec: v[crt.nod]) {
muchie &mvec = muchii[vec];
if(mvec.cf <= 0)
continue;
int newd = mind[crt.nod] + mvec.cost + bfd[crt.nod] - bfd[mvec.b];
if(newd < mind[mvec.b]) {
mind[mvec.b] = newd;
new_bfd[mvec.b] = new_bfd[crt.nod] + mvec.cost;
from[mvec.b] = vec;
pq.push(elem(mvec.b, newd));
}
}
}
swap(bfd, new_bfd);
return vizd;
}
void add_flux() {
int fmin = INF;
for(int nod = d; from[nod] != -1; nod = muchii[from[nod]].a)
fmin = min(fmin, muchii[from[nod]].cf);
ctot += fmin * bfd[d];
for(int nod = d; from[nod] != -1; nod = muchii[from[nod]].a) {
muchii[from[nod]].cf -= fmin;
muchii[from[nod] ^ 1].cf += fmin;
}
}
int main() {
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
scanf("%d %d %d %d", &n, &m, &s, &d);
for(int i = 1; i <= m; i++) {
int x, y, z, t;
scanf("%d %d %d %d", &x, &y, &z, &t);
muchii[2 * i] = muchie(x, y, z, t);
v[x].push_back(2 * i);
muchii[2 * i + 1] = muchie(x, y, 0, -t);
v[y].push_back(2 * i + 1);
}
bellman_ford(s);
while(dijkstra(s))
add_flux();
printf("%d", ctot);
return 0;
}