#include <bits/stdc++.h>
using namespace std;
struct node {
  long long u, v, next, w, p, c;
} edge[400005];
long long cnt, nxt[200005], sum[200005];
void link(long long u, long long v, long long w, long long p) {
  edge[cnt] = {u, v, nxt[u], w, p, 0};
  nxt[u] = cnt++;
}
void dfs(long long u) {
  long long i, v, tmp;
  for (i = nxt[u]; i != -1; i = edge[i].next) {
    v = edge[i].v;
    dfs(v);
    sum[u] += sum[v];
    tmp = min(min(edge[i].w - 1, edge[i].p), edge[i].p - sum[v]);
    if (tmp < 0) {
      puts("-1");
      exit(0);
    }
    edge[i].w -= tmp;
    edge[i].p -= tmp;
    edge[i].c = tmp;
    sum[u] += edge[i].w;
  }
}
long long dfs2(long long u, long long x) {
  long long use = 0, i, v, tmp, val;
  for (i = nxt[u]; i != -1; i = edge[i].next) {
    v = edge[i].v;
    tmp = min(edge[i].c, x);
    edge[i].w += tmp;
    edge[i].p += tmp;
    x -= tmp;
    use += tmp;
    val = dfs2(v, min(x, edge[i].p - sum[v]));
    x -= val;
    use += val;
  }
  return use;
}
int main() {
  long long i, n, a, b, c, d;
  cin >> n;
  memset(nxt, -1, sizeof(nxt));
  for (i = 1; i < n; i++) {
    cin >> a >> b >> c >> d;
    link(a, b, c, d);
  }
  dfs(1);
  dfs2(1, 1ll << 60);
  cout << n << endl;
  for (i = 0; i < cnt; i++)
    cout << edge[i].u << " " << edge[i].v << " " << edge[i].w << " "
         << edge[i].p << endl;
  return 0;
}
