#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const long long INF = 1e18;
long long power(long long x, long long y) {
  long long t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}
vector<int> G[N];
vector<long long> snd[N];
long long sub[N], sum[N], red[N], dol[N];
long long mns[N], mxs[N];
int lol[N];
vector<pair<int, pair<long long, long long> > > edges;
void done() {
  printf("-1\n");
  exit(0);
}
void dfs(int v) {
  for (int i = 0; i < G[v].size(); ++i) {
    int ed = G[v][i];
    dfs(edges[ed].first);
    long long &w = edges[ed].second.first, &p = edges[ed].second.second;
    if (mns[edges[ed].first] > p) done();
    long long z = min(w - 1, p - mns[edges[ed].first]);
    mns[v] += mns[edges[ed].first] + w - z;
    snd[v].push_back(mns[edges[ed].first] + w - z);
    mxs[v] += w + min(p, mxs[edges[ed].first]);
    assert(mns[v] <= mxs[v]);
  }
}
void dfs1(int v, long long nd) {
  for (int i = 0; i < G[v].size(); ++i) nd -= snd[v][i];
  for (int i = 0; i < G[v].size(); ++i) {
    int ed = G[v][i], to = edges[ed].first;
    long long &w = edges[ed].second.first, &p = edges[ed].second.second;
    long long z = min(w - 1, p - mns[edges[ed].first]);
    w -= z;
    p -= z;
    long long k = min(nd, z);
    w += k;
    p += k;
    nd -= k;
    k = min(min(nd, p - mns[edges[ed].first]),
            mxs[edges[ed].first] - mns[edges[ed].first]);
    dfs1(to, mns[edges[ed].first] + k);
    nd -= k;
  }
  assert(nd == 0);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    long long w, p;
    scanf("%d%d%lld%lld", &x, &y, &w, &p);
    x--;
    y--;
    G[x].push_back(edges.size());
    lol[i] = x;
    edges.push_back(make_pair(y, make_pair(w, p)));
  }
  dfs(0);
  dfs1(0, mxs[0]);
  printf("%d\n", n);
  for (int i = 0; i < n - 1; ++i) {
    printf("%d %d %lld %lld\n", lol[i] + 1, edges[i].first + 1,
           edges[i].second.first, edges[i].second.second);
  }
  return 0;
}
