#include <bits/stdc++.h>
using namespace std;
namespace FastIO {
char buf[1 << 23], *p1 = buf, *p2 = buf;
inline int read() {
  static int x, f;
  static char ch;
  x = 0, f = 1;
  ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
            ? EOF
            : *p1++);
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  return x * f;
}
inline long long readll() {
  static long long x = 0;
  static int f;
  static char ch;
  x = 0, f = 1;
  ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
            ? EOF
            : *p1++);
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  return x * f;
}
void write(int x) {
  if (!x) putchar('0');
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  static int sta[20];
  int tot = 0;
  while (x) {
    sta[tot++] = x % 10;
    x /= 10;
  }
  while (tot) putchar(sta[--tot] + 48);
}
}  // namespace FastIO
using FastIO::read;
using FastIO::readll;
using FastIO::write;
const int INF = 0x3f3f3f3f;
const long long llINF = 1e18;
const int MAXN = 2e5 + 5;
int n, len, flag = 1;
int head[MAXN], dep[MAXN];
long long sumW[MAXN], rem[MAXN], sumRem[MAXN], newW[MAXN], newP[MAXN];
priority_queue<pair<int, int> > q[MAXN];
struct Road {
  int u, v;
} edge[MAXN];
struct Edge {
  int to, next;
  long long w, p;
} e[MAXN << 1];
void add(int u, int v, long long w, long long p) {
  e[++len].to = v;
  e[len].next = head[u];
  e[len].w = w;
  e[len].p = p;
  head[u] = len;
}
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  for (int i = head[u]; i != -1; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    dfs(v, u);
    sumW[u] += sumW[v] + e[i].w;
    sumRem[u] += sumRem[v];
    if (q[v].size() > q[u].size()) swap(q[v], q[u]);
    while (!q[v].empty()) {
      q[u].push(q[v].top());
      q[v].pop();
    }
  }
  if (u != 1) {
    while (newP[u] < sumW[u]) {
      if (q[u].empty()) {
        printf("-1\n");
        exit(0);
      }
      int id = q[u].top().second;
      long long tmp = min(sumW[u] - newP[u], rem[id]);
      newW[id] -= tmp;
      newP[id] -= tmp;
      sumW[u] -= tmp;
      rem[id] -= tmp;
      sumRem[u] -= tmp;
      if (rem[id] <= 0) q[u].pop();
    }
    if (newP[u] < sumW[u]) {
      printf("-1\n");
      exit(0);
    }
    q[u].push(make_pair(dep[u], u));
    rem[u] = min(newW[u] - 1, newP[u] - sumW[u] + sumRem[u]);
    sumRem[u] += rem[u];
  }
}
int main() {
  memset(head, -1, sizeof(head));
  n = read();
  for (int i = 1; i < n; i++) {
    int u = read(), v = read(), w = read(), p = read();
    newW[v] = w;
    newP[v] = p;
    edge[i] = (Road){u, v};
    add(u, v, w, p);
  }
  dfs(1, 0);
  printf("%d\n", n);
  for (int i = 1; i < n; i++)
    printf("%d %d %lld %lld\n", edge[i].u, edge[i].v, newW[edge[i].v],
           newP[edge[i].v]);
  return 0;
}
