#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
struct EG {
  int a, b, w, p;
  void gt() { scanf("%d%d%d%d", &a, &b, &w, &p); }
} E[(200009)];
struct st {
  int C, I;
  bool operator<(const st &r) const { return C > r.C; }
};
int C[(200009)];
priority_queue<st> Q[(200009)];
int gc(int a) { return C[a] = (a == C[a] ? a : gc(C[a])); }
void con(int a, int b) {
  if (gc(a) == gc(b)) return;
  if (Q[gc(a)].size() < Q[gc(b)].size()) swap(a, b);
  while (!Q[gc(b)].empty()) Q[gc(a)].push(Q[gc(b)].top()), Q[gc(b)].pop();
  C[C[b]] = C[a];
}
vector<pair<int, int> > g[(200009)];
long long T[(200009)], U[(200009)], D[(200009)];
int N, G[(200009)] = {-1, -1}, R, P[(200009)] = {-1, -1}, t, m;
void cl(int u, int p) {
  for (auto &h : g[u])
    if (h.first ^ p) P[h.first] = u, G[h.first] = h.second, cl(h.first, u);
}
void ud(int u, int v) {
  if ((0)) printf("SUB: %d < %d\n", u, v);
  U[P[u]] += v, E[G[u]].w -= v, E[G[u]].p -= v, m = min(U[P[u]], D[P[u]]),
                                                U[P[u]] -= m, D[P[u]] -= m;
}
void dfs(int u, int p) {
  for (auto &h : g[u])
    if (h.first ^ p) dfs(h.first, u), con(u, h.first);
  if ((0)) printf("%d: (%lld+%d)%lld\n", u, T[u], E[G[u]].w, T[u] + E[G[u]].w);
  if (u ^ 1 && T[u] > E[G[u]].p) {
    D[u] = T[u] - E[G[u]].p;
    while (!Q[gc(u)].empty() && T[u] > E[G[u]].p) {
      t = Q[gc(u)].top().I;
      if ((0))
        printf(" >%d: (%lld)%lld\n", t, T[u],
               (min(E[G[t]].p - T[t] + U[t], E[G[t]].w - 1ll)));
      if (T[u] - E[G[u]].p - (min(E[G[t]].p - T[t] + U[t], E[G[t]].w - 1ll)) >
          0)
        T[u] -= (min(E[G[t]].p - T[t] + U[t], E[G[t]].w - 1ll)),
            ud(t, (min(E[G[t]].p - T[t] + U[t], E[G[t]].w - 1ll))),
            Q[gc(u)].pop(), m = min(D[P[t]], U[P[t]] += U[t]), D[P[t]] -= m,
                            U[P[t]] -= m;
      else
        ud(t, T[u] - E[G[u]].p), T[u] = E[G[u]].p;
    }
    if (T[u] > E[G[u]].p) puts("-1"), exit(0);
  }
  if ((0))
    printf("  %d: (%lld+%d)%lld\n", u, T[u], E[G[u]].w, T[u] + E[G[u]].w);
  if (~p) T[p] += T[u] + E[G[u]].w;
  Q[gc(u)].push({R++, u});
}
int main(void) {
  scanf("%d", &N);
  for (int i(0); i < N - 1; i++)
    E[i].gt(), g[E[i].a].push_back({E[i].b, i}),
        g[E[i].b].push_back({E[i].a, i});
  iota(C, C + (200009), 0), cl(1, -1), dfs(1, -1);
  printf("%d\n", N);
  for (int i(0); i < N - 1; i++)
    printf("%d %d %d %d\n", E[i].a, E[i].b, E[i].w, E[i].p);
  return 0;
}
