#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
const int MAXN = 200111;
const long long INF = 1LL << 60;
int N;
vector<int> G[MAXN];
long long P[MAXN], W[MAXN], sumW[MAXN];
int par[MAXN];
long long low[MAXN], high[MAXN];
long long ans[MAXN];
long long dp[MAXN];
long long C[MAXN];
vector<int> ord;
int main() {
  scanf("%d", &N);
  for (int i = 0, i_len = (N - 1); i < i_len; ++i) {
    int x, y, w, p;
    scanf("%d%d%d%d", &x, &y, &w, &p);
    x--;
    y--;
    G[x].push_back(y);
    par[y] = x;
    P[y] = p;
    W[y] = w;
    C[i] = y;
  }
  P[0] = INF;
  W[0] = 0;
  ord.reserve(N);
  ord.push_back(0);
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    int v = ord[i];
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e)
      ord.push_back(*e);
  }
  bool yes = true;
  for (int i_ = 0, i__len = (N); i_ < i__len; ++i_) {
    int v = ord[N - 1 - i_];
    low[v] = high[v] = 0;
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e) {
      long long de = min(P[*e] - low[*e], W[*e] - 1);
      low[v] += low[*e] + W[*e] - de;
      high[v] += high[*e] + W[*e];
      sumW[v] += sumW[*e] + W[*e];
    }
    amin(high[v], P[v]);
    if (high[v] < low[v]) {
      yes = false;
      break;
    }
    if (P[v] < low[v]) {
      yes = false;
      break;
    }
  }
  if (!yes) {
    puts("-1");
    return 0;
  }
  dp[0] = high[0];
  for (int i_ = 0, i__len = (N); i_ < i__len; ++i_) {
    int v = ord[i_];
    long long re = dp[v];
    if (v) {
      long long de = min(P[v] - low[v], W[v] - 1);
      amin(de, (P[v] - re + W[v]) / 2);
      ans[v] += W[v] - de;
      re -= W[v] - de;
    }
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e) {
      long long de = min(P[*e] - low[*e], W[*e] - 1);
      long long tmp = low[*e] + W[*e] - de;
      dp[*e] += tmp;
      re -= tmp;
    }
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e) {
      long long de = min(P[*e] - low[*e], W[*e] - 1);
      long long tmp_low = low[*e] + W[*e] - de;
      long long tmp_high = high[*e] + W[*e];
      long long tmp = tmp_high - tmp_low;
      amin(tmp, re);
      dp[*e] += tmp;
      re -= tmp;
    }
    ans[v] += re;
  }
  printf("%d\n", N);
  for (int i = 0, i_len = (N - 1); i < i_len; ++i) {
    int v = C[i];
    printf("%d %d %lld %lld\n", par[v] + 1, v + 1, ans[v],
           P[v] - (W[v] - ans[v]));
  }
  return 0;
}
