#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long u, v, w, s;
  edge() {
    u = 0;
    v = 0;
    w = 0;
    s = 0;
  }
};
long long n, ans, dpmax[200010], dpmin[200010], dpsum[200010];
long long wg[200010], st[200010], tod[200010], idx[200010];
edge e[200010];
vector<vector<long long> > gr;
void dfs(long long x) {
  for (auto i : gr[x]) {
    dfs(i);
    long long s = st[i], w = wg[i];
    dpmin[x] += dpmin[i] + w - min(w - 1, s - dpmin[i]);
    dpmax[x] += w + dpmax[i];
    dpsum[x] += w + dpsum[i];
  }
  dpmax[x] = min(dpmax[x], st[x]);
  if (dpmin[x] > st[x]) {
    cout << -1;
    exit(0);
  }
}
void tr(long long u, long long v, long long val) {
  tod[u] -= val;
  tod[v] += val;
}
void dfsgreedy(long long x) {
  for (auto i : gr[x]) {
    tr(x, i, dpsum[i] - dpmax[i]);
  }
  for (auto i : gr[x]) {
    tr(x, i, min(tod[x], dpmax[i] - dpmin[i]));
  }
  for (auto i : gr[x]) {
    long long w = wg[i], s = st[i];
    long long tosub = min(tod[x], min(w - 1, s - dpmin[i]));
    e[idx[i]].w -= tosub;
    e[idx[i]].s -= tosub;
    tod[x] -= tosub;
    dfsgreedy(i);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  vector<long long> vi;
  gr.resize(n + 1, vi);
  for (long long i = 0; i < n - 1; ++i) {
    cin >> e[i].u >> e[i].v >> e[i].w >> e[i].s;
    long long u = e[i].u, v = e[i].v;
    idx[v] = i;
    gr[u].push_back(v);
    wg[v] = e[i].w;
    st[v] = e[i].s;
  }
  st[1] = 1e18;
  wg[1] = 1;
  dfs(1);
  tod[1] = dpsum[1] - dpmax[1];
  dfsgreedy(1);
  cout << n << "\n";
  for (long long i = 0; i < n - 1; ++i) {
    cout << e[i].u << " " << e[i].v << " " << e[i].w << " " << e[i].s << "\n";
  }
}
