#include <bits/stdc++.h>
using namespace std;
long long W[200001];
pair<long long, long long> M[200001];
pair<pair<int, int>, pair<int, int> > ed[200000];
vector<int> tr[200001];
inline void no() {
  printf("-1\n");
  exit(0);
}
void pl(int v = 1) {
  pair<long long, long long> &m = M[v];
  for (int i = 0; i < (int)tr[v].size(); ++i) {
    int e = tr[v][i], u = ed[e].first.second, w = ed[e].second.first,
        p = ed[e].second.second;
    pl(u);
    if (p < M[u].first) no();
    W[v] += W[u] + w;
    m.second += min(M[u].second, (long long)p) + w;
    long long sp = max(0ll, min(p - M[u].first, w - 1ll));
    m.first += M[u].first + w - sp;
  }
}
int rm(int v = 1, long long need = W[1] - M[1].second) {
  long long cw = 0;
  for (int i = 0; i < (int)tr[v].size(); ++i) {
    int e = tr[v][i], u = ed[e].first.second, p = ed[e].second.second;
    need -= W[u] - min((long long)p, M[u].second);
  }
  for (int i = 0; i < (int)tr[v].size(); ++i) {
    int e = tr[v][i], u = ed[e].first.second, w = ed[e].second.first,
        p = ed[e].second.second;
    long long t = min(min((long long)p, M[u].second) - M[u].first, need);
    long long an = rm(u, t + W[u] - min((long long)p, M[u].second));
    cw += an;
    long long z = min((long long)p, M[u].second) - an;
    need -= z;
    int d = min(need, min(w - 1ll, p - an));
    need -= d;
    ed[e].second.second -= d;
    ed[e].second.first -= d;
    cw += w - d;
  }
  return cw;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < (int)n; ++i) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    tr[a].push_back(i);
    ed[i] = make_pair(make_pair(a, b), make_pair(c, d));
  }
  pl();
  rm();
  printf("%d\n", n);
  for (int i = 1; i < (int)n; ++i)
    printf("%d %d %d %d\n", ed[i].first.first, ed[i].first.second,
           ed[i].second.first, ed[i].second.second);
}
