#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
std::vector<long long> w, p;
std::vector<long long> minw, maxw;
std::vector<long long> needw;
std::vector<long long> curw;
vector<std::vector<std::pair<int, int> > > nb;
std::vector<std::vector<int> > ch;
int n;
std::vector<int> par;
bool can = true;
void dfs(int v) {
  for (auto u : ch[v]) dfs(u);
  minw[v] = 0;
  maxw[v] = 0;
  curw[v] = 0;
  for (auto u : ch[v]) {
    maxw[v] += maxw[u];
    minw[v] += minw[u];
    curw[v] += curw[u];
  }
  if (v == 0) return;
  int e = par[v];
  curw[v] += w[e];
  if (p[e] < minw[v]) {
    cout << -1;
    exit(0);
  }
  long long dim = min(w[e] - 1, p[e] - minw[v]);
  minw[v] += w[e] - dim;
  maxw[v] = min(maxw[v], p[e]);
  maxw[v] += w[e];
}
void dim(int v, long long to) {
  for (auto u : ch[v]) {
    needw[u] = curw[u];
    if (needw[u] > maxw[u]) {
      curw[v] -= (needw[u] - maxw[u]);
      needw[u] = maxw[u];
    }
  }
  for (auto u : ch[v]) {
    long long rem = min(needw[u] - minw[u], curw[v] - to);
    needw[u] -= rem;
    curw[v] -= rem;
    dim(u, needw[u]);
  }
  w[par[v]] -= curw[v] - to;
  p[par[v]] -= curw[v] - to;
  curw[v] = to;
}
int main() {
  scanf("%d", &n);
  w.resize(n - 1);
  p.resize(n - 1);
  par.resize(n);
  nb.resize(n);
  ch.resize(n);
  minw.resize(n);
  curw.resize(n);
  maxw.resize(n);
  std::vector<int> u1(n), u2(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d %d %lld %lld", &x, &y, &w[i], &p[i]);
    u1[i] = x;
    u2[i] = y;
    x--;
    y--;
    nb[x].push_back(make_pair(y, i));
    nb[y].push_back(make_pair(x, i));
  }
  std::vector<std::pair<int, int> > q;
  q.push_back(make_pair(0, 0));
  int pt = 0;
  while (pt < q.size()) {
    int v = q[pt].first;
    for (auto u : nb[v]) {
      if (u.first != q[pt].second) {
        par[u.first] = u.second;
        ch[v].push_back(u.first);
        q.push_back(make_pair(u.first, v));
      }
    }
    pt++;
  }
  dfs(0);
  needw.resize(n);
  for (auto v : ch[0]) dim(v, min(curw[v], maxw[v]));
  printf("%d\n", n);
  for (int i = 0; i < n - 1; i++) {
    printf("%d %d %lld %lld\n", u1[i], u2[i], w[i], p[i]);
  }
}
