#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int nmax = 355;
const int inf = 0x3f3f3f3f;
struct Edge {
int u, v, cap, flow, ind, cost;
Edge(int _u, int _v, int _cap, int _flow, int _ind, int _cost) {
u = _u;
v = _v;
cap = _cap;
flow = _flow;
ind = _ind;
cost = _cost;
}
};
vector<Edge> E;
vector<int> g[nmax];
int n, m, k, source, sink;
int father[nmax], dist[nmax];
bool inqueue[nmax];
vector<vector<int> > ans;
long long total;
bool bellmanford() {
for (int i = 1; i <= n; ++ i) {
inqueue[i] = 0;
father[i] = -1;
dist[i] = 2000000001;
}
queue<int> q;
q.push(source);
dist[source] = 0;
inqueue[source] = 1;
while (!q.empty()) {
int node = q.front();
q.pop();
inqueue[node] = 0;
for (int ind : g[node]) {
Edge e = E[ind];
if (e.cap > e.flow && dist[e.u] + e.cost < dist[e.v]) {
dist[e.v] = dist[e.u] + e.cost;
father[e.v] = ind;
if (!inqueue[e.v]) {
inqueue[e.v] = 1;
q.push(e.v);
}
}
}
}
return dist[sink] != 2000000001;
}
void maxflow() {
long long flow = 0;
while (true) {
if (!bellmanford()) {
break;
}
int minflow = inf, node = sink;
while (node != source) {
Edge e = E[father[node]];
minflow = min(minflow, e.cap - e.flow);
node = e.u;
}
node = sink;
while (node != source) {
Edge &e = E[father[node]];
Edge &re = E[father[node] ^ 1];
e.flow += minflow;
re.flow -= minflow;
node = e.u;
}
total += minflow * dist[sink];
flow += minflow;
}
printf("%lld\n", total);
}
int main() {
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
scanf("%i %i %i %i", &n, &m, &source, &sink);
for (int i = 1; i <= m; ++ i) {
int x, y, c, z;
scanf("%i %i %i %i", &x, &y, &c, &z);
E.push_back(Edge(x, y, c, 0, i, z));
g[x].push_back(E.size() - 1);
E.push_back(Edge(y, x, 0, 0, i, -z));
g[y].push_back(E.size() - 1);
}
maxflow();
return 0;
}