// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = vector<Edge>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

class Graph {
  vector<Edges> g;
  using iterator = vector<Edges>::iterator;
  using const_iterator = vector<Edges>::const_iterator;

public:
  Graph(const int &size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  const Edges &operator[](const int &i) const { return g[i]; }
  Edges &operator[](const int &i) { return g[i]; }
  void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int &node1, const int &node2, const Weight &w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

class Dijkstra {
  int src;
  Graph g;
  vector<Weight> dist;
  vector<int> prev;

public:
  Dijkstra(const Graph &g, const int &src, const Weight inf = numeric_limits<Weight>::max() / 8) //
      : g(g), src(src), dist(g.size(), inf), prev(g.size()) {
    using state = pair<Weight, int>;
    priority_queue<state, vector<state>, greater<state>> q;
    dist[src] = 0;
    q.emplace(0, src);
    while (q.size()) {
      Weight d;
      int v;
      tie(d, v) = q.top();
      q.pop();
      if (dist[v] < d) continue;
      for (auto &e : g[v]) {
        if (cmin(dist[e.dst], dist[v] + e.weight)) {
          prev[e.dst] = v;
          q.emplace(dist[e.dst], e.dst);
        }
      }
    }
  }

  Weight shortestDist(const int &v) { return dist[v]; }

  vector<int> shortestPath(int v) {
    vector<int> p;
    while (v != src) {
      p.emplace_back(v);
      v = prev[v];
    }
    p.emplace_back(src);
    reverse(all(p));
    return p;
  }

  Graph shortestPathDAG(const int &destination) {
    Graph rg(g.size());
    for (auto &es : g) {
      for (auto &e : es) rg.addArc(e.dst, e.src, e.weight);
    }
    Dijkstra rdij(rg, destination);
    Graph dag(g.size());
    Weight st = shortestDist(destination);
    for (auto &es : g) {
      for (auto &e : es) {
        Weight su = shortestDist(e.src), uv = e.weight, vt = rdij.shortestDist(e.dst);
        if (su + uv + vt == st) dag.addArc(e.src, e.dst, e.weight);
      }
    }
    return dag;
  }
};

using Flow = int;
struct FlowEdge {
  int src, dst;
  Flow cap;
  int rev;
  FlowEdge(const int &s = 0, const int &d = 0, const Flow &c = 0, const int &r = 0) : src(s), dst(d), cap(c), rev(r) {}
};

using FlowEdges = vector<FlowEdge>;

class FlowGraph {
  vector<FlowEdges> g;
  using iterator = vector<FlowEdges>::iterator;
  using const_iterator = vector<FlowEdges>::const_iterator;

public:
  FlowGraph(const int &size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  FlowEdges &operator[](const int &i) & { return g[i]; }

  void addEdge(const int &src, const int &dst, const Flow &c = 1) {
    g[src].emplace_back(src, dst, c, g[dst].size());
    g[dst].emplace_back(dst, src, 0, g[src].size() - 1);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

// Change it to 'FlowGraph &g' when you need a residual network
template<Flow inf = numeric_limits<Flow>::max() / 8> Flow dinic(FlowGraph g, const int &source, const int &sink) {
  vector<int> level(g.size()), iter(g.size());
  auto bfs = [&] {
    fill(level.begin(), level.end(), -1);
    queue<int> q;
    level[source] = 0;
    q.push(source);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (auto &e : g[v]) {
        if (level[e.dst] >= 0 || e.cap <= 0) continue;
        level[e.dst] = level[v] + 1;
        q.push(e.dst);
      }
    }
    return level[sink] >= 0;
  };
  function<Flow(int, Flow)> dfs = [&](int v, Flow f) -> Flow {
    if (v == sink) return f;
    Flow s = 0;
    for (; iter[v] < g[v].size(); iter[v]++) {
      auto &e = g[v][iter[v]];
      if (level[v] >= level[e.dst] || e.cap <= 0) continue;
      Flow d = dfs(e.dst, min(f, e.cap));
      e.cap -= d;
      g[e.dst][e.rev].cap += d;
      s += d;
      f -= d;
      if (f == 0) break;
    }
    return s;
  };
  Flow s = 0;
  while (bfs()) {
    fill(iter.begin(), iter.end(), 0);
    s += dfs(source, inf);
  }
  return s;
}

main {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  --s, --t;
  Graph g(n);
  auto cost = vectors(n, n, INF);
  while (m--) {
    int u, v, d, c;
    cin >> u >> v >> d >> c;
    --u, --v;
    g.addArc(u, v, d);
    cmin(cost[u][v], c);
  }
  Dijkstra dij(g, s);
  FlowGraph h(n);
  for (auto &es : dij.shortestPathDAG(t)) {
    for (auto &e : es) h.addEdge(e.src, e.dst, cost[e.src][e.dst]);
  }
  cout << dinic(h, s, t) << endl;
}



Measured memory_bytes: 3392