#include <bits/stdc++.h>
const int N = 200005;
const long long inf = 0x3f3f3f3f3f3f3f3f;
struct edge {
  int to, nxt, weight, strength;
} e[N << 1];
int n, head[N], tot = 1;
void link(int x, int y, int z, int w) {
  e[++tot] = (edge){y, head[x], z, w}, head[x] = tot;
  e[++tot] = (edge){x, head[y], z, w}, head[y] = tot;
}
long long x[N], y[N], z[N], w[N], d[N], id[N];
long long sum[N];
long long dfs0(int x, int f = 0) {
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != f) id[e[i].to] = i >> 1, sum[x] += dfs0(e[i].to, x);
  if (sum[x] > w[id[x]]) std::cout << "-1\n", std::exit(0);
  d[x] = std::min(w[id[x]] - sum[x], z[id[x]] - 1);
  return sum[x] + z[id[x]] - d[x];
}
long long dfs1(int x, int f = 0, long long rem = inf) {
  long long t = std::min(rem, d[x]), ans = t;
  d[x] -= t, rem -= t;
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      long long r = dfs1(e[i].to, x, std::min(w[id[x]] - d[x] - sum[x], rem));
      rem -= r, ans += r, sum[x] += r;
    }
  return ans;
}
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n, w[0] = inf;
  for (int i = 1; i < n; ++i) {
    std::cin >> x[i] >> y[i] >> z[i] >> w[i];
    link(x[i], y[i], z[i], w[i]);
  }
  dfs0(1), dfs1(1);
  std::cout << n << '\n';
  for (int i = 1; i <= n; ++i) z[id[i]] -= d[i], w[id[i]] -= d[i];
  for (int i = 1; i < n; ++i)
    std::cout << x[i] << ' ' << y[i] << ' ' << z[i] << ' ' << w[i] << '\n';
  return 0;
}
