#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10;
const long long MN = 2e5 + 10;
const long long BIG = 1e18 + 5;
long long n;
vector<pair<long long, long long> > adj[MN];
long long es[MN], ee[MN], ew[MN], ep[MN];
long long lim[MN];
long long Mn[MN];
long long Sz[MN];
void fail() {
  cout << -1 << '\n';
  exit(0);
}
long long prep(long long s, long long pr) {
  long long ret = 0;
  for (auto x : adj[s])
    if (x.first != pr) {
      long long v = x.first, e = x.second;
      long long val = prep(v, s);
      if (val > ep[e]) fail();
      long long t = min(ep[e] - val, ew[e] - 1);
      ew[e] -= t, ep[e] -= t;
      ret += val + ew[e];
    }
  Sz[s] = ret;
  return ret;
}
long long dfs(long long s, long long pr, long long cur) {
  long long ret = 0;
  for (auto x : adj[s])
    if (x.first != pr) {
      long long v = x.first, e = x.second;
      long long val = min(cur, lim[e] - ew[e]);
      ew[e] += val, ep[e] += val;
      long long tmp = dfs(v, s, min((cur - val), ep[e] - Sz[v]));
      ret += val + tmp;
      cur -= tmp + val;
    }
  return ret;
}
int32_t main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n - 1; ++i) {
    cin >> es[i] >> ee[i] >> ew[i] >> ep[i];
    lim[i] = ew[i];
    --es[i], --ee[i];
    adj[es[i]].push_back({ee[i], i}), adj[ee[i]].push_back({es[i], i});
  }
  prep(0, -1);
  dfs(0, -1, BIG);
  cout << n << '\n';
  for (long long i = 0; i < n - 1; ++i)
    cout << es[i] + 1 << ' ' << ee[i] + 1 << ' ' << ew[i] << ' ' << ep[i]
         << '\n';
  return 0;
}
