#include <bits/stdc++.h>
using namespace std;
template <class _Tp>
void ckmax(_Tp &a, _Tp b) {
  if (a < b) a = b;
}
template <class _Tp>
void ckmin(_Tp &a, _Tp b) {
  if (a > b) a = b;
}
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
  return (b == 0) ? (a) : (gcd(b, a % b));
}
int read() {
  char ch = getchar();
  bool f = 1;
  int x = 0;
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') f = 0, ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch & 15);
    ch = getchar();
  }
  return f ? x : -x;
}
const int inf = 1000000000;
const long long Inf = 1000000000000000000ll;
struct Edge {
  int to, nxt, frm;
  long long p, w, c;
} e[200050 << 1];
int Top, head[200050];
void add(int u, int v, long long w, long long p) {
  e[++Top].to = v;
  e[Top].nxt = head[u];
  head[u] = Top;
  e[Top].p = p;
  e[Top].w = w;
  e[Top].frm = u;
}
bool tag;
long long siz[200050];
void dfs1(int u) {
  for (int i = head[u]; i; i = e[i].nxt) {
    if (tag) return;
    dfs1(e[i].to);
    long long t = min(min(e[i].w - 1, e[i].p), e[i].p - siz[e[i].to]);
    if (t < 0) {
      tag = 1;
      return;
    }
    e[i].p -= t;
    e[i].w -= t;
    e[i].c = t;
    siz[u] += siz[e[i].to] + e[i].w;
  }
}
long long dfs2(int u, long long val) {
  long long delta = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    long long t = min(val, e[i].c);
    e[i].w += t;
    e[i].p += t;
    delta += t;
    val -= t;
    long long sons = dfs2(e[i].to, min(val, e[i].p - siz[e[i].to]));
    delta += sons;
    val -= sons;
  }
  return delta;
}
int n;
int main() {
  n = read();
  for (int i = 1; i < n; ++i) {
    int x = read(), y = read(), w = read(), p = read();
    add(x, y, w, p);
  }
  dfs1(1);
  if (tag) {
    puts("-1");
    return 0;
  }
  dfs2(1, Inf);
  printf("%d\n", n);
  for (int i = 1; i <= Top; ++i)
    printf("%d %d %lld %lld\n", e[i].frm, e[i].to, e[i].w, e[i].p);
  return 0;
}
