#include <bits/stdc++.h>
using namespace std;
int x[200002], y[200002], w[200002], p[200002], W[200002], P[200002];
long long dp[200002];
vector<int> g[200002];
bool dfs(int u, int par) {
  for (int i = 0; i < g[u].size(); i++) {
    int idx = g[u][i];
    int v = x[idx];
    if (v == u) v = y[idx];
    if (v == par) continue;
    if (!dfs(v, u)) return 0;
    if (p[idx] >= dp[v]) {
      int mn = p[idx] - dp[v];
      mn = min(mn, w[idx] - 1);
      mn = min(mn, p[idx]);
      w[idx] -= mn;
      p[idx] -= mn;
    } else
      return 0;
    dp[u] += dp[v];
    dp[u] += w[idx];
  }
  return 1;
}
long long dfs2(int u, int par, long long add) {
  long long res = 0;
  for (int i = 0; i < g[u].size(); i++) {
    int idx = g[u][i];
    int v = x[idx];
    if (v == u) v = y[idx];
    if (v == par) continue;
    long long d = min(P[idx] - p[idx], W[idx] - w[idx]);
    d = min(d, add);
    res += d;
    p[idx] += d;
    w[idx] += d;
    add -= d;
    long long cur = min(add, P[idx] - dp[v]);
    long long tmp = dfs2(v, u, cur);
    add -= tmp;
    res += tmp;
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d%d%d", &x[i], &y[i], &w[i], &p[i]);
    W[i] = w[i];
    P[i] = p[i];
    g[x[i]].emplace_back(i);
    g[y[i]].emplace_back(i);
  }
  bool ok = dfs(1, 0);
  if (!ok)
    printf("-1\n");
  else {
    dfs2(1, 0, 1e18);
    printf("%d\n", n);
    for (int i = 1; i <= n - 1; i++)
      printf("%d %d %d %d\n", x[i], y[i], w[i], p[i]);
  }
}
