#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, mod = 1e9 + 7, inf = 1e8;
const long long inff = 1e16;
int par[maxn];
long long w[maxn], s[maxn], subsum[maxn], tmp[maxn];
vector<int> adj[maxn];
vector<pair<int, int>> vec;
bool mark[maxn];
long long dfs(int v) {
  mark[v] = 1;
  bool flg = 1;
  for (auto u : adj[v])
    if (!mark[u]) {
      flg = 0;
      subsum[v] += dfs(u);
    }
  if (s[v] < subsum[v]) {
    cout << "-1\n";
    exit(0);
  }
  tmp[v] = min(s[v] - subsum[v], w[v] - 1);
  s[v] -= tmp[v];
  w[v] -= tmp[v];
  return subsum[v] + w[v];
}
long long dfs2(int v, long long sub) {
  mark[v] = 1;
  long long save = sub;
  for (auto u : adj[v])
    if (!mark[u]) {
      long long mn = min(sub, tmp[u]);
      sub -= mn;
      w[u] += mn;
      s[u] += mn;
      long long newsub = min(sub, s[u] - subsum[u]);
      sub -= dfs2(u, newsub);
    }
  return save - sub;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, x, y;
    cin >> u >> v >> x >> y;
    u--, v--;
    vec.push_back({u, v});
    par[v] = u;
    w[v] = x;
    s[v] = y;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  s[0] = inff;
  dfs(0);
  memset(mark, 0, sizeof mark);
  s[0] = inff;
  dfs2(0, inff);
  cout << n << "\n";
  for (auto x : vec)
    cout << x.first + 1 << " " << x.second + 1 << " " << w[x.second] << " "
         << s[x.second] << "\n";
  return 0;
}
