#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 350;
const int INF = 0x3f3f3f3f;
vector<int> graph[1+MAX_N];
int kappa[1+MAX_N][1+MAX_N], cost[1+MAX_N][1+MAX_N], cost2[1+MAX_N][1+MAX_N];
int dist[1+MAX_N], dist2[1+MAX_N], papa[1+MAX_N], realdist[1+MAX_N];
bool inq[1+MAX_N];
deque<int> q;
static inline void pushQ(int nod) {
if(!inq[nod]) {
inq[nod] = true;
q.push_back(nod);
}
}
static inline int frontq() {
int nod = q.front();
q.pop_front();
inq[nod] ^= 1;
return nod;
}
void bellman(int n, int s) {
for(int i = 1; i <= n; ++i)
dist[i] = INF;
dist[s] = 0;
pushQ(s);
while(!q.empty()) {
int nod = frontq();
for(auto it: graph[nod])
if(kappa[nod][it] > 0 && dist[nod] + cost[nod][it] < dist[it]) {
dist[it] = dist[nod] + cost[nod][it];
pushQ(it);
}
}
}
int maxflow, mincost;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
static inline int min(int a, int b) {
if(a < b)
return a;
return b;
}
static inline bool augment(int s, int d) {
memset(dist2, 0x3f, sizeof(dist));
dist2[s] = 0;
realdist[s] = 0;
pq.push(make_pair(0, s));
while(!pq.empty()) {
pair<int,int> shit = pq.top();
pq.pop();
int nod = shit.second, distm = shit.first;
if(distm == dist2[nod])
for(auto it: graph[nod]) {
int val = dist[nod] + cost[nod][it] - dist[it];
if(kappa[nod][it] > 0 && distm + val < dist2[it]) {
dist2[it] = distm + val;
realdist[it] = realdist[nod] + cost[nod][it];
papa[it] = nod;
pq.push(make_pair(dist2[it], it));
}
}
}
memcpy(dist, realdist, sizeof(dist));
if(dist2[d] < INF) {
int nod = d, fl = INF;
while(nod != s) {
fl = min(fl, kappa[papa[nod]][nod]);
nod = papa[nod];
}
nod = d;
mincost += realdist[d] * fl;
while(nod != s) {
kappa[papa[nod]][nod] -= fl;
kappa[nod][papa[nod]] += fl;
nod = papa[nod];
}
return true;
}
return false;
}
int main() {
int n, m, s, d, x, y, z, c;
FILE *fin = fopen("fmcm.in", "r");
fscanf(fin, "%d%d%d%d", &n, &m, &s, &d);
for(int i = 0; i < m; ++i) {
fscanf(fin, "%d%d%d%d", &x, &y, &c, &z);
graph[x].push_back(y);
graph[y].push_back(x);
kappa[x][y] = c;
cost[x][y] = z;
cost[y][x] = -z;
}
fclose(fin);
bellman(n, s);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cost2[i][j] = dist[i] + cost[i][j] - dist[j];
while(augment(s, d));
FILE *fout = fopen("fmcm.out", "w");
fprintf(fout, "%d", mincost);
fclose(fout);
return 0;
}