#include <bits/stdc++.h>
using namespace std;
int n, wei[200005], strong[200005], pick[200005], par[200005];
vector<int> v[200005];
long long sub[200005], mapsum[200005], used[200005], ans[200005];
struct info {
  int depth, idx;
  info(int a, int b) { depth = a, idx = b; }
  bool operator<(const info &i) const {
    if (depth != i.depth) return depth > i.depth;
    return idx > i.idx;
  }
};
map<info, long long> M[200005];
int root(int p) {
  if (par[p] == p) return p;
  return par[p] = root(par[p]);
}
void uni(int a, int b) {
  if (a == b) return;
  par[a] = b;
}
void merge(map<info, long long> &M1, map<info, long long> &M2) {
  auto it = M1.begin();
  while (it != (M1).end()) {
    (M2)[it->first] = it->second;
    ++it;
  }
}
void dfs(int now, int dep) {
  for (int i = 0; i < v[now].size(); i++) {
    int next = v[now][i];
    dfs(next, dep + 1);
    mapsum[now] += mapsum[next];
    int R = root(now);
    int R2 = root(next);
    if ((M[R]).size() < (M[R2]).size()) {
      merge(M[R], M[R2]);
      uni(R, R2);
    } else {
      merge(M[R2], M[R]);
      uni(R2, R);
    }
    sub[now] += sub[next];
  }
  if (now == 1) return;
  int R = root(now);
  auto it = (M[R]).begin();
  while (it != (M[R]).end()) {
    if (strong[now] < sub[now] - it->second) {
      mapsum[now] -= it->second;
      sub[now] -= it->second;
      used[it->first.idx] += it->second;
      it = (M[R]).erase(it);
    } else {
      long long gap = sub[now] - strong[now];
      if (gap < 0) break;
      mapsum[now] -= gap;
      sub[now] -= gap;
      used[it->first.idx] += gap;
      it->second -= gap;
      if (it->second == 0) (M[R]).erase(it);
      break;
    }
  }
  if (strong[now] < (sub[now] - mapsum[now])) {
    puts("-1");
    exit(0);
  }
  long long cutting = min((long long)min(wei[now] - 1, strong[now]),
                          (long long)strong[now] - (sub[now] - mapsum[now]));
  if (cutting < 0) {
    puts("-2");
    exit(0);
  }
  sub[now] += wei[now];
  (M[R])[info(dep, now)] = cutting;
  mapsum[now] += cutting;
}
int x[200005], y[200005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d %d", &x[i], &y[i]);
    v[x[i]].push_back(y[i]);
    scanf("%d %d", &wei[y[i]], &strong[y[i]]);
    pick[y[i]] = i;
    par[i] = i;
  }
  par[n] = n;
  dfs(1, 0);
  for (int i = 2; i <= n; i++) {
    ans[pick[i]] = used[i];
  }
  printf("%d\n", n);
  for (int i = 1; i <= n - 1; i++) {
    int pp = wei[y[i]] - ans[i];
    int tt = (strong[y[i]] - ans[i]);
    printf("%d %d %d %d\n", x[i], y[i], pp, tt);
  }
  for (int i = 1; i <= n; i++) M[i].clear();
}
