#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline T parse(const string &s) {
  T x;
  stringstream ss(s);
  ss >> x;
  return x;
}
const double EPS = 1e-12;
const int INF = 1000 * 1000 * 1000;
const long long LINF = INF * 1ll * INF;
const double DINF = 1e200;
const double PI = 3.1415926535897932384626433832795l;
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long gcdex(long long a, long long b, long long &x, long long &y) {
  if (!a) {
    x = 0, y = 1;
    return b;
  }
  long long k = b / a;
  long long g = gcdex(b - k * a, a, y, x);
  x -= k * y;
  return g;
}
long long inv(long long a, long long m) {
  assert(m > 1);
  long long x, y, g;
  g = gcdex(a, m, x, y);
  return (x % (m / g) + m / g) % m / g;
}
long long crt(long long a1, long long m1, long long a2, long long m2) {
  long long a = (a2 - a1 % m2 + m2) % m2;
  long long x, y, g;
  g = gcdex(m1, m2, x, y);
  if (a % g) return -1;
  long long m = m1 / g * m2;
  assert(x + m2 >= 0);
  x = a / g * (x + m2) % m2;
  long long r = (a1 + x * m1) % m;
  assert(r % m1 == a1 && r % m2 == a2);
  return r;
}
long long powmod(long long a, long long p, long long m) {
  assert(p >= 0);
  long long r = 1;
  while (p) {
    if (p & 1) r = r * a % m;
    p >>= 1;
    a = a * a % m;
  }
  return r;
}
bool isprime(long long a) {
  if (a <= 1) return false;
  for (long long i = 2; i * i <= a; ++i) {
    if (a % i == 0) return false;
  }
  return true;
}
long long sqrtup(long long a) {
  if (!a) return 0;
  long long x = max(0ll, (long long)sqrt((double)a));
  while (x * x >= a) --x;
  while ((x + 1) * (x + 1) < a) ++x;
  return x + 1;
}
long long isqrt(long long a) {
  assert(a >= 0);
  long long x = max(0ll, (long long)sqrt((double)a));
  while (x * x > a) --x;
  while ((x + 1) * (x + 1) <= a) ++x;
  return x;
}
long long sgn(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; }
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v);
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p);
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m);
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a);
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t);
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v) {
  s << '[';
  for (int i = 0; i < (((int)(v).size())); ++i) {
    if (i) s << ',';
    s << v[i];
  }
  s << ']';
  return s;
}
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m) {
  s << "{";
  bool f = false;
  for (const auto &it : m) {
    if (f) s << ",";
    f = true;
    s << it.first << ": " << it.second;
  }
  s << "}";
  return s;
}
template <class T>
ostream &operator<<(ostream &s, const set<T> &m) {
  s << "{";
  bool f = false;
  for (const auto &it : m) {
    if (f) s << ",";
    f = true;
    s << it;
  }
  s << "}";
  return s;
}
template <class T>
ostream &operator<<(ostream &s, const multiset<T> &m) {
  s << "{";
  bool f = false;
  for (const auto &it : m) {
    if (f) s << ",";
    f = true;
    s << it;
  }
  s << "}";
  return s;
}
template <class T, class V, class C>
ostream &operator<<(ostream &s, const priority_queue<T, V, C> &q) {
  auto a = q;
  s << "{";
  bool f = false;
  while (!a.empty()) {
    if (f) s << ",";
    f = true;
    s << a.top();
    a.pop();
  }
  return s << "}";
}
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a) {
  s << '[';
  for (int i = 0; i < (((int)(a).size())); ++i) {
    if (i) s << ',';
    s << a[i];
  }
  s << ']';
  return s;
}
template <size_t n, class... T>
struct put1 {
  static ostream &put(ostream &s, const tuple<T...> &t) {
    s << get<sizeof...(T) - n>(t);
    if (n > 1) s << ',';
    return put1<n - 1, T...>::put(s, t);
  }
};
template <class... T>
struct put1<0, T...> {
  static ostream &put(ostream &s, const tuple<T...> &t) { return s; }
};
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t) {
  s << "(";
  put1<sizeof...(T), T...>::put(s, t);
  s << ")";
  return s;
}
ostream &put3(ostream &s, const char *, bool) { return s; }
template <class U, class... T>
ostream &put3(ostream &s, const char *f, bool fs, U &&u, T &&...t) {
  while (*f == ' ') ++f;
  if (!fs) s << ", ";
  auto nf = f;
  int d = 0;
  while (*nf && (*nf != ',' || d)) {
    if (*nf == '(')
      ++d;
    else if (*nf == ')')
      --d;
    ++nf;
  }
  auto nf2 = nf;
  while (nf2 > f && *(nf2 - 1) == ' ') --nf;
  fs = *f == '"';
  if (!fs) {
    s.write(f, nf2 - f);
    s << "=";
  };
  s << u;
  if (fs) s << ' ';
  if (*nf) ++nf;
  return put3(s, nf, fs, forward<T>(t)...);
}
struct edge {
  int v, i, q;
  long long c, f;
};
ostream &operator<<(ostream &o, edge e) {
  return o << "(v:" << e.v << ",i:" << e.i << ",q:" << e.q << ",c:" << e.c
           << ",f:" << e.f << ")";
}
struct graph {
  vector<vector<edge>> G;
  vector<char> W;
  int S, T;
  graph(int n) : G(n) {}
  void add(int a, int b, int c, int q) {
    edge e1{b, ((int)(G[b]).size()), q, c, 0};
    edge e2{a, ((int)(G[a]).size()), q, 0, 0};
    G[a].push_back(e1);
    G[b].push_back(e2);
  }
  int dfs(int v) {
    if (v == T) return 1;
    if (W[v]) return 0;
    W[v] = 1;
    for (edge &e : G[v]) {
      if (e.f == e.c || !dfs(e.v)) continue;
      edge &e2 = G[e.v][e.i];
      ++e.f;
      --e2.f;
      return 1;
    }
    return 0;
  }
  int flow() {
    for (int f = 0; f < (INF); ++f) {
      W.assign(((int)(G).size()), 0);
      if (!dfs(S)) return f;
      assert(f < 100000);
    }
    return -1;
  }
};
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  int n, m, S, T;
  cin >> n >> m >> S >> T;
  --S;
  --T;
  vector<pair<pair<int, int>, int>> E(m);
  for (auto &e : E) {
    cin >> e.first.first >> e.first.second >> e.second;
    --e.first.first;
    --e.first.second;
  }
  graph g1(n + 2);
  g1.S = n;
  g1.T = n + 1;
  g1.add(T, S, INF, -1);
  for (int i = 0; i < (((int)(E).size())); ++i) {
    if (!E[i].second) continue;
    g1.add(E[i].first.first, E[i].first.second, INF, i);
    g1.add(E[i].first.first, g1.T, 1, -1);
    g1.add(g1.S, E[i].first.second, 1, -1);
  }
  g1.flow();
  for (int v = 0; v < (n); ++v) {
    for (edge &e : g1.G[v]) {
      if (e.q == -1 || e.f <= 0) continue;
      E[e.q].second += e.f;
    }
  }
  do {
  } while (false);
  graph g2(n);
  g2.S = S;
  g2.T = T;
  for (int i = 0; i < (((int)(E).size())); ++i) {
    if (E[i].second) {
      g2.add(E[i].first.first, E[i].first.second, 1, i);
      g2.add(E[i].first.second, E[i].first.first, INF, 0);
    } else
      g2.add(E[i].first.first, E[i].first.second, INF, i);
  }
  do {
  } while (false);
  int r = g2.flow();
  set<int> sat;
  for (int v = 0; v < (n); ++v) {
    if (!g2.W[v]) continue;
    for (edge &e : g2.G[v]) {
      if (!e.c || g2.W[e.v]) continue;
      assert(e.c != INF && e.f == e.c);
      assert(e.q != -1);
      assert(!sat.count(e.q));
      sat.insert(e.q);
    }
  }
  assert(((int)(sat).size()) == r);
  cout << r << '\n';
  for (int i = 0; i < (((int)(E).size())); ++i) {
    long long f = E[i].second;
    long long c = f + !sat.count(i);
    cout << f << ' ' << c << '\n';
  }
  return 0;
}
