#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
struct Edge {
  long long x, y, w, p;
} E[maxn], edge[maxn];
long long w[maxn], ww[maxn];
int n;
vector<int> G[maxn];
void dfs1(int x, int y, int id) {
  for (int i = 0; i < G[y].size(); i++) {
    int nextid = G[y][i];
    dfs1(edge[nextid].x, edge[nextid].y, nextid);
  }
  if (id == 0) return;
  if (edge[id].p < w[y]) {
    puts("-1");
    exit(0);
  }
  long long temp = min(edge[id].w - 1, edge[id].p - w[y]);
  edge[id].p -= temp;
  edge[id].w -= temp;
  w[x] += w[y] + edge[id].w;
}
void dfs2(int x, int y, int id, int val) {
  if (id != 0) {
    long long temp = min((long long)val, E[id].p - edge[id].p);
    edge[id].p += temp;
    edge[id].w += temp;
    val = min((long long)(val - temp), edge[id].p - w[y]);
    ww[id] = temp;
  }
  for (int i = 0; i < G[y].size(); i++) {
    int nextid = G[y][i];
    if (id == 0) val = 2e9;
    dfs2(edge[nextid].x, edge[nextid].y, nextid, val);
    val -= ww[nextid];
    ww[id] += ww[nextid];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%lld%lld%lld%lld", &edge[i].x, &edge[i].y, &edge[i].w, &edge[i].p);
    E[i] = edge[i];
    G[edge[i].x].push_back(i);
  }
  dfs1(0, 1, 0);
  dfs2(0, 1, 0, 2e9);
  printf("%d\n", n);
  for (int i = 1; i < n; i++) {
    printf("%lld %lld %lld %lld\n", edge[i].x, edge[i].y, edge[i].w, edge[i].p);
  }
  return 0;
}
