#include <bits/stdc++.h>
const size_t ioSize = 1 << 21;
char iBuf[ioSize], *iS, *iE;
inline char getc() {
  return iS == iE ? iE = iBuf + fread(iS = iBuf, 1, ioSize, stdin),
                    iS == iE ? EOF : *iS++ : *iS++;
}
void readi(int &x) {
  char c;
  for (c = getc(); !isdigit(c); c = getc())
    ;
  x = c ^ '0';
  for (c = getc(); isdigit(c); c = getc()) (x *= 10) += c ^ '0';
}
const size_t maxN = 200005;
int N;
struct edge_t {
  int from, to, a, b, d;
  edge_t *next;
} * Head[maxN], edgeBuf[maxN], *edgeCur(edgeBuf);
inline void addEdge(int u, int v, int a, int b) {
  *edgeCur = (edge_t){u, v, a, b, 0, Head[u]};
  Head[u] = edgeCur++;
}
long long sumA[maxN];
void DFS1(int i) {
  for (edge_t *j = Head[i]; j; j = j->next) {
    DFS1(j->to);
    sumA[i] += sumA[j->to];
    long long t =
        std::min({(long long)j->a - 1, (long long)j->b, j->b - sumA[j->to]});
    if (t < 0) {
      puts("-1");
      std::exit(0);
    }
    j->a -= t, j->b -= t, j->d = t;
    sumA[i] += j->a;
  }
}
long long DFS2(int i, long long rest) {
  long long res = 0;
  for (edge_t *j = Head[i]; j; j = j->next) {
    long long t = std::min(rest, (long long)j->d);
    j->a += t, j->b += t, rest -= t, res += t;
    t = DFS2(j->to, std::min(rest, j->b - sumA[j->to]));
    rest -= t, res += t;
  }
  return res;
}
int main() {
  readi(N);
  for (int u, v, a, b, i = 1; i != N; ++i) {
    readi(u), readi(v), readi(a), readi(b);
    addEdge(u, v, a, b);
  }
  DFS1(1);
  DFS2(1, 0x7F7F7F7F7F7F7F7FLL);
  printf("%d\n", N);
  for (edge_t *e = edgeBuf; e != edgeCur; ++e)
    printf("%d %d %d %d\n", e->from, e->to, e->a, e->b);
  return 0;
}
