#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void priv(vector<int> a) {
  for (int i = 0; i < ((int)(a).size()); ++i)
    printf("%d%c", a[i], i == (int)(a).size() - 1 ? '\n' : ' ');
}
template <typename T>
istream& operator>>(istream& i, vector<T>& v) {
  for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j];
  return i;
}
template <typename T>
string join(const vector<T>& v) {
  stringstream s;
  for (int i = 0; i < ((int)(v).size()); ++i) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  if ((int)(v).size()) o << join(v);
  return o;
}
template <typename T1, typename T2>
istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& v) {
  return o << v.first << "," << v.second;
}
const int MX = 200005, INF = 1001001001;
const long long int LINF = 1e18;
const double eps = 1e-10;
const int M = 1000000000;
struct Maxflow {
  int n;
  vector<int> to, next, head, dist, it;
  vector<int> lim;
  Maxflow() {}
  Maxflow(int n) : n(n), head(n, -1), it(n) {}
  void add(int a, int b, int c = 1) {
    next.push_back(head[a]);
    head[a] = (int)(to).size();
    to.push_back(b);
    lim.push_back(c);
    next.push_back(head[b]);
    head[b] = (int)(to).size();
    to.push_back(a);
    lim.push_back(0);
  }
  void bfs(int sv) {
    dist = vector<int>(n, INF);
    queue<int> q;
    dist[sv] = 0;
    q.push(sv);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i = head[v]; i != -1; i = next[i]) {
        if (lim[i] && dist[to[i]] == INF) {
          dist[to[i]] = dist[v] + 1;
          q.push(to[i]);
        }
      }
    }
  }
  int dfs(int v, int tv, int nf = INF) {
    if (v == tv) return nf;
    for (; it[v] != -1; it[v] = next[it[v]]) {
      int u = to[it[v]];
      int f;
      if (!lim[it[v]] || dist[v] >= dist[u]) continue;
      if (f = dfs(u, tv, min(nf, lim[it[v]])), f) {
        lim[it[v]] -= f;
        lim[it[v] ^ 1] += f;
        return f;
      }
    }
    return 0;
  }
  int solve(int sv, int tv) {
    int flow = 0, f;
    while (1) {
      bfs(sv);
      if (dist[tv] == INF) return flow;
      for (int i = 0; i < (n); ++i) it[i] = head[i];
      while (f = dfs(sv, tv), f) flow += f;
    }
  }
  vector<int> used;
  set<pair<int, int> > get(int v) {
    used = vector<int>(n);
    queue<int> q;
    used[v] = 1;
    q.push(v);
    while ((int)(q).size()) {
      v = q.front();
      q.pop();
      for (int i = head[v]; i != -1; i = next[i]) {
        int u = to[i];
        if (!lim[i]) continue;
        if (used[u]) continue;
        used[u] = 1;
        q.push(u);
      }
    }
    set<pair<int, int> > res;
    for (int v = 0; v < (n); ++v) {
      if (!used[v]) continue;
      for (int i = head[v]; i != -1; i = next[i]) {
        int u = to[i];
        if (used[u]) continue;
        if (i % 2 == 0) res.insert(pair<int, int>(v, u));
      }
    }
    return res;
  }
};
int main() {
  int n, m, sv, tv;
  scanf("%d%d%d%d", &n, &m, &sv, &tv);
  --sv;
  --tv;
  vector<vector<int> > es;
  for (int i = 0; i < (m); ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a;
    --b;
    es.push_back({a, b, c});
  }
  Maxflow g(n);
  for (int i = 0; i < (m); ++i) {
    int a = es[i][0];
    int b = es[i][1];
    int c = es[i][2];
    g.add(a, b, c ? 1 : INF);
    if (c) g.add(b, a, INF);
  }
  int ans = g.solve(sv, tv);
  set<pair<int, int> > mc = g.get(sv);
  assert((int)(mc).size() == ans);
  for (int i = 0; i < (m); ++i) {
    int a = es[i][0];
    int b = es[i][1];
    if (mc.count(pair<int, int>(a, b))) {
      es[i][2] = -1;
    }
  }
  vector<int> x(m);
  vector<vector<int> > pre(n, vector<int>(n, INF));
  vector<vector<int> > nxt(n, vector<int>(n, INF));
  {
    vector<vector<int> > to(n), id(n);
    for (int i = 0; i < (m); ++i) {
      int a = es[i][0];
      int b = es[i][1];
      int c = es[i][2];
      if (!c) continue;
      to[a].push_back(b);
      id[a].push_back(i);
    }
    for (int r = 0; r < (n); ++r) {
      queue<int> q;
      pre[r][r] = -1;
      q.push(r);
      while ((int)(q).size()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < ((int)(to[v]).size()); ++i) {
          int u = to[v][i];
          if (pre[r][u] != INF) continue;
          pre[r][u] = id[v][i];
          q.push(u);
        }
      }
    }
  }
  {
    vector<vector<int> > to(n), id(n);
    for (int i = 0; i < (m); ++i) {
      int a = es[i][0];
      int b = es[i][1];
      int c = es[i][2];
      if (!c) continue;
      to[b].push_back(a);
      id[b].push_back(i);
    }
    for (int r = 0; r < (n); ++r) {
      queue<int> q;
      nxt[r][r] = -1;
      q.push(r);
      while ((int)(q).size()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < ((int)(to[v]).size()); ++i) {
          int u = to[v][i];
          if (nxt[r][u] != INF) continue;
          nxt[r][u] = id[v][i];
          q.push(u);
        }
      }
    }
  }
  for (int i = 0; i < (m); ++i) {
    int a = es[i][0];
    int b = es[i][1];
    int c = es[i][2];
    if (!c) continue;
    if (x[i]) continue;
    bool done = false;
    {
      int j = i;
      int r = sv;
      if (pre[b][a] != INF) r = b, done = true;
      while (j != -1) {
        x[j]++;
        j = pre[r][es[j][0]];
      }
    }
    if (!done) {
      int j = i;
      int r = tv;
      while (j != -1) {
        x[j]++;
        j = nxt[r][es[j][1]];
      }
      x[i]--;
    }
  }
  printf("%d\n", ans);
  for (int i = 0; i < (m); ++i) {
    int c = es[i][2];
    if (c) {
      if (c == -1)
        printf("%d %d\n", x[i], x[i]);
      else
        printf("%d %d\n", x[i], M);
    } else {
      printf("%d %d\n", 0, M);
    }
  }
  return 0;
}
