#include <bits/stdc++.h>
#define MAXN 350
#define INF 1000000000
int n, m, S, D, total;
std::vector <int> G[1 + MAXN];
int C[1 + MAXN][1 + MAXN], Cost[1 + MAXN][1 + MAXN];
int father[1 + MAXN];
int q[1 + MAXN * MAXN], p, u, inq[1 + MAXN];
int dist[1 + MAXN];
inline void bellmanFord(){
for(int i = 1; i <= n; i++) dist[i] = INF;
p = u = 0;
q[u++] = S, inq[S] = 1, dist[S] = 0;
while(p < u){
int node = q[p++];
inq[node] = 0;
for(auto y:G[node])
if(C[node][y] && dist[y] > dist[node] + Cost[node][y]){
dist[y] = dist[node] + Cost[node][y];
if(!inq[y]) inq[y] = 1, q[u++] = y;
}
}
}
int d[1 + MAXN], reald[1 + MAXN];
std::priority_queue <std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;
inline bool dijkstra(){
for(int i = 1; i <= n; i++) d[i] = INF;
d[S] = 0, reald[S] = 0;
pq.push({0, S});
while(!pq.empty()){
int node = pq.top().second, cost = pq.top().first;
pq.pop();
if(d[node] == cost){
for(auto y: G[node])
if(C[node][y]){
int newd = d[node] + Cost[node][y] + dist[node] - dist[y];
if(newd < d[y]){
d[y] = newd;
reald[y] = reald[node] + Cost[node][y];
father[y] = node;
pq.push({d[y], y});
}
}
}
}
memcpy(dist, reald, n);
return (d[D] != INF);
}
int main(){
FILE*fi,*fo;
fi = fopen("fmcm.in","r");
fo = fopen("fmcm.out","w");
fscanf(fi,"%d%d%d%d", &n, &m, &S, &D);
for(int i = 1; i <= m; i++){
int x, y, capacity, cost;
fscanf(fi,"%d%d%d%d", &x, &y, &capacity, &cost);
G[x].push_back(y);
G[y].push_back(x);
C[x][y] += capacity;
Cost[x][y] += cost;
Cost[y][x] -= cost;
}
bellmanFord();
while(dijkstra()){
int flow = INF, cost = 0;
for(int i = D; i != S; i = father[i]) flow = std::min(flow, C[father[i]][i]), cost += Cost[father[i]][i];
for(int i = D; i != S; i = father[i]) C[father[i]][i] -= flow, C[i][father[i]] += flow;
total += cost * flow;
}
fprintf(fo,"%d", total);
return 0;
}