#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int h[MAX];
long long w[MAX], sum[MAX];
struct aa {
  int next;
  int from;
  int to;
  long long w;
  long long p;
  long long d;
} a[MAX];
int cnt = 0;
void add() {
  cnt++;
  scanf("%d%d%I64d%I64d", &a[cnt].from, &a[cnt].to, &a[cnt].w, &a[cnt].p);
  a[cnt].next = h[a[cnt].from];
  h[a[cnt].from] = cnt;
}
void dfs(int x) {
  for (int i = h[x]; i; i = a[i].next) {
    int y = a[i].to;
    dfs(y);
    long long temp = min(a[i].w - 1, a[i].p - sum[y]);
    sum[x] += sum[y];
    if (temp < 0) {
      cout << -1 << endl;
      exit(0);
    }
    a[i].w -= temp;
    a[i].p -= temp;
    a[i].d = temp;
    sum[x] += a[i].w;
  }
}
long long dfs(int x, long long s) {
  long long used = 0;
  for (int i = h[x]; i; i = a[i].next) {
    int y = a[i].to;
    long long b = min(s, a[i].d);
    a[i].w += b;
    a[i].p += b;
    used += b;
    s -= b;
    long long temp = dfs(y, min(s, a[i].p - sum[y]));
    s -= temp;
    used += temp;
  }
  return used;
}
int main() {
  int n, m, i, j;
  scanf("%d", &n);
  for (i = 1; i < n; i++) add();
  dfs(1);
  dfs(1, 1ll << 60);
  printf("%d\n", n);
  for (i = 1; i < n; i++) {
    printf("%d %d %I64d %I64d\n", a[i].from, a[i].to, a[i].w, a[i].p);
  }
  return 0;
}
