/*#include<stdio.h>
#include<vector>
#include<string.h>
#include<queue>
#define inf "fmcm.in"
#define outf "fmcm.out"
#define NMax 351
#define INF 0x3f3f3f3f
using namespace std;
int N, M, S, D;
vector<int> G[NMax];
int C[NMax][NMax], Cost[NMax][NMax], F[NMax][NMax];
int go;
int Dist[NMax], From[NMax], in[NMax];
void read()
{
scanf("%d%d%d%d", &N, &M, &S, &D);
int x, y, c, z;
for(int i=1; i<=M; i++) {
scanf("%d%d%d%d", &x, &y, &c, &z);
G[x].push_back(y);
G[y].push_back(x);
C[x][y] = c;
Cost[x][y] = z;
Cost[y][x] = -z;
}
}
int BF() {
for(int i=1; i<=N; i++) { Dist[i] = INF; From[i] = -1; }
Dist[S] = 0;
memset( in, 0, sizeof(in) );
queue<int> q;
q.push(S); in[S] = 1;
while( !q.empty() ) {
int u = q.front(); q.pop();
in[u] = 0;
for(int i=0; i<G[u].size(); i++) {
int v = G[u][i];
if( C[u][v] - F[u][v] > 0 && Dist[u] + Cost[u][v] < Dist[v] ) {
Dist[v] = Dist[u] + Cost[u][v];
From[v] = u;
if( !in[v] ) { in[v] = 1; q.push(v); }
}
}
}
if( Dist[D]!=INF ) {
go = 1;
int fmin = INF, u;
for( u=D; u!=S; u=From[u] ) fmin = min( fmin, C[From[u]][u] - F[From[u]][u] );
for( u=D; u!=S; u=From[u] ) {
F[From[u]][u] += fmin;
F[u][From[u]] -= fmin;
}
return fmin*Dist[D];
}
return 0;
}
void solve()
{
go = 1; long long rez = 0;
while( go ) {
go = 0;
rez += BF();
}
printf("%lld", rez);
}
int main()
{
freopen(inf,"r",stdin); freopen(outf,"w",stdout);
read(); solve();
return 0;
}*/
#include <stdio.h>
#include <vector>
#include<string.h>
using namespace std;
#define maxn 510
#define inf 2000000000
#define ll long long
#define maxx 1000010
int N, M, S, D;
int Drum, L;
vector <int> A[maxn];
int G[maxn], Dist[maxn], From[maxn];
int Q[maxx], U[maxn];
int C[maxn][maxn], F[maxn][maxn], Cost[maxn][maxn];
int BellmanFord()
{
int i, j;
for (i = 1; i <= N; i++)
{
Dist[i] = inf;
From[i] = -1;
}
Dist[S] = 0;
L = 1;
Q[L] = S;
memset(U, 0, sizeof(U));
U[S] = 1;
for (i = 1; i <= L; i++)
{
for (j=0; j<G[Q[i]]; j++)
if (C[Q[i]][A[Q[i]][j]]-F[Q[i]][A[Q[i]][j]]>0 && Dist[Q[i]]+Cost[Q[i]][A[Q[i]][j]]<Dist[A[Q[i]][j]])
{
Dist[A[Q[i]][j]] = Dist[Q[i]] + Cost[Q[i]][A[Q[i]][j]];
From[A[Q[i]][j]] = Q[i];
if (!U[A[Q[i]][j]])
{
Q[++L] = A[Q[i]][j];
U[Q[L]] = 1;
}
}
U[Q[i]] = 0;
}
if (Dist[D] != inf)
{
int Vmin = inf;
Drum = 1;
for (i = D; i != S; i = From[i]) Vmin = min(Vmin, C[From[i]][i] - F[From[i]][i]);
for (i = D; i != S; i = From[i])
{
F[From[i]][i] += Vmin;
F[i][From[i]] -= Vmin;
}
return Vmin * Dist[D];
}
return 0;
}
ll Flux()
{
ll Rez = 0;
Drum = 1;
while (Drum)
{
Drum = 0;
Rez += BellmanFord();
}
return Rez;
}
int main()
{
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
int i, x, y, z, cap;
scanf("%d %d %d %d ", &N, &M, &S, &D);
for (i = 1; i <= M; i++)
{
scanf("%d %d %d %d ", &x, &y, &cap, &z);
A[x].push_back(y);
A[y].push_back(x);
C[x][y] = cap;
Cost[x][y] = z;
Cost[y][x] = -z;
}
for (i = 1; i <= N; i++) G[i] = A[i].size();
printf("%lld\n", Flux());
return 0;
}