#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Edge {
  long long u, v, w, p, nxt;
} edge[N];
int tot, head[N];
void init() {
  tot = 0;
  memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int p) {
  edge[tot].u = u;
  edge[tot].v = v;
  edge[tot].w = w;
  edge[tot].p = p;
  edge[tot].nxt = head[u];
  head[u] = tot++;
}
long long dpmin[N], dpmax[N], dpsum[N];
bool flag;
void dfs0(int u) {
  if (!flag) return;
  dpmin[u] = dpmax[u] = dpsum[u] = 0;
  for (int i = head[u]; i != -1; i = edge[i].nxt) {
    int v = edge[i].v;
    dfs0(v);
    if (edge[i].p < dpmin[v]) {
      flag = false;
      return;
    }
    dpmax[v] = min(dpmax[v], edge[i].p);
    dpmin[u] += dpmin[v] + edge[i].w - min(edge[i].p - dpmin[v], edge[i].w - 1);
    dpmax[u] += dpmax[v] + edge[i].w;
    dpsum[u] += dpsum[v] + edge[i].w;
  }
}
void dfs1(int u, long long s, int in) {
  long long x = s;
  for (int i = head[u]; i != -1; i = edge[i].nxt) {
    int v = edge[i].v;
    x -= dpsum[v] - dpmax[v];
  }
  for (int i = head[u]; i != -1; i = edge[i].nxt)
    if (x > 0) {
      int v = edge[i].v;
      long long t =
          dpsum[v] - dpmin[v] + min(edge[i].p - dpmin[v], edge[i].w - 1);
      long long k =
          dpmax[v] - dpmin[v] + min(edge[i].p - dpmin[v], edge[i].w - 1);
      dfs1(v, min(x, k) + dpsum[v] - dpmax[v], i);
      x -= k;
      s -= t;
    } else {
      int v = edge[i].v;
      dfs1(v, dpsum[v] - dpmax[v], i);
      s -= dpsum[v] - dpmax[v];
    }
  if (s > 0) {
    edge[in].w -= s;
    edge[in].p -= s;
  }
}
int main() {
  int n;
  long long u, v, w, p;
  scanf("%d", &n);
  init();
  for (int i = 1; i < n; i++) {
    scanf("%lld%lld%lld%lld", &u, &v, &w, &p);
    addedge(u, v, w, p);
  }
  flag = true;
  dfs0(1);
  if (flag) dfs1(1, dpsum[1] - dpmax[1], -1);
  if (flag) {
    printf("%d\n", n);
    for (int i = 0; i < n - 1; i++)
      printf("%lld %lld %lld %lld\n", edge[i].u, edge[i].v, edge[i].w,
             edge[i].p);
  } else
    printf("-1\n");
  return 0;
}
