#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6;
vector<long long> adj[N];
long long w[N], p[N], sum[N], can[N], sum_can[N];
priority_queue<pair<long long, long long> > no[N];
bool failed;
long long dfs(long long u, long long L) {
  for (long long v : adj[u]) {
    sum[u] += dfs(v, L + 1) + w[v];
    sum_can[u] += sum_can[v];
    if (no[v].size() > no[u].size()) swap(no[v], no[u]);
    while (!no[v].empty()) {
      no[u].push(no[v].top());
      no[v].pop();
    }
  }
  if (!L) return 0;
  while (sum[u] > p[u] && !no[u].empty()) {
    long long _, v;
    tie(_, v) = no[u].top();
    long long got = min(sum[u] - p[u], can[v]);
    assert(got >= 0);
    p[v] -= got;
    w[v] -= got;
    sum[u] -= got;
    can[v] -= got;
    sum_can[u] -= got;
    if (can[v] == 0) no[u].pop();
  }
  if (sum[u] > p[u]) failed = 1;
  no[u].push({L, u});
  can[u] = max(0LL, min(w[u] - 1, p[u] - sum[u] + sum_can[u]));
  sum_can[u] += can[u];
  return sum[u];
}
int32_t main() {
  long long n;
  cin >> n;
  vector<pair<long long, long long> > e;
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    cin >> w[v] >> p[v];
    adj[u].push_back(v);
    e.push_back({u, v});
  }
  dfs(1, 0);
  if (failed)
    cout << -1 << endl;
  else {
    cout << n << endl;
    for (pair<long long, long long> pt : e) {
      cout << pt.first << " " << pt.second << " " << w[pt.second] << " "
           << p[pt.second] << endl;
    }
  }
}
