#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 5;
char buf[1 << 19], *p1 = buf, *p2 = buf;
int read() {
  int ret = 0, f = 1;
  char ch =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 19, stdin), p1 == p2)
           ? EOF
           : *p1++);
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 19, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  for (; isdigit(ch);
       ch = (p1 == p2 &&
                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 19, stdin), p1 == p2)
                 ? EOF
                 : *p1++))
    ret = ret * 10 + ch - 48;
  return ret * f;
}
int n, U[200005], V[200005];
long long a[200005], b[200005], sum[200005], Can[200005], sumc[200005];
struct node {
  int dep, u;
};
priority_queue<node> Q[200005];
bool operator<(node a, node b) { return a.dep < b.dep; }
vector<int> G[200005];
void dfs(int u, int Dep) {
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    dfs(v, Dep + 1);
    sum[u] += sum[v] + a[v];
    sumc[u] += sumc[v];
    if (Q[v].size() > Q[u].size()) swap(Q[u], Q[v]);
    while (Q[v].size()) {
      Q[u].push(Q[v].top());
      Q[v].pop();
    }
  }
  if (u == 1) return;
  while (b[u] < sum[u]) {
    if (Q[u].empty()) {
      cout << -1;
      exit(0);
    }
    node now = Q[u].top();
    Q[u].pop();
    int minn = min(sum[u] - b[u], Can[now.u]);
    a[now.u] -= minn, b[now.u] -= minn, Can[now.u] -= minn, sum[u] -= minn,
        sumc[u] -= minn;
    if (Can[now.u] > 0) Q[u].push(now);
  }
  if (b[u] < sum[u]) {
    cout << -1;
    exit(0);
  }
  Q[u].push(node{Dep, u});
  Can[u] = min(a[u] - 1, b[u] - sum[u] + sumc[u]);
  sumc[u] += Can[u];
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> U[i] >> V[i];
    G[U[i]].push_back(V[i]);
    cin >> a[V[i]] >> b[V[i]];
  }
  dfs(1, 0);
  cout << n << endl;
  for (int i = 1; i < n; i++)
    cout << U[i] << " " << V[i] << " " << a[V[i]] << " " << b[V[i]] << endl;
  return 0;
}
