#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<ll>;
using vii = vector<ii>;
const int MX = 2 * 100007;
int n;
ii weis[MX], edge[MX], par[MX];
int rr[MX], rrl[MX];
vector<ii> g[MX];
list<int> ee[MX];
ll w[MX], wl[MX], wd[MX];
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, w, s;
    cin >> a >> b >> w >> s;
    a--;
    b--;
    edge[i] = {a, b};
    weis[i] = {w, s};
    g[a].push_back({b, i});
    rr[a]++;
    rrl[a]++;
    par[b] = {a, i};
  }
  queue<ll> q;
  for (int i = 0; i < n; i++)
    if (!rr[i]) q.push(i);
  bool ok = 1;
  while (q.size()) {
    int v = q.front();
    q.pop();
    if (v == 0) break;
    int p = par[v].first, pe = par[v].second;
    while (ee[v].size() && weis[pe].second < w[v]) {
      ll ei = ee[v].front();
      ll md = min(weis[ei].first - 1, weis[ei].second - wl[edge[ei].second]);
      if (md <= 0) {
        ee[v].pop_front();
        wd[edge[ei].first] += wd[edge[ei].second];
        rrl[edge[ei].first]--;
        if (!rrl[edge[ei].first]) {
          wl[edge[ei].first] += wd[edge[ei].first];
        }
        continue;
      }
      ll diff = w[v] - weis[pe].second;
      ll mmd = min(md, diff);
      weis[ei].first -= mmd;
      weis[ei].second -= mmd;
      w[v] -= mmd;
      wd[edge[ei].first] -= mmd;
    }
    if (weis[pe].second < w[v]) {
      ok = 0;
      break;
    }
    ee[p].splice(ee[p].end(), ee[v]);
    ee[p].push_back(pe);
    w[p] += w[v] + weis[pe].first;
    wl[p] += wl[v] + weis[pe].first;
    rr[p]--;
    if (!rr[p]) q.push(p);
  }
  if (!ok)
    cout << -1 << endl;
  else {
    cout << n << endl;
    for (int i = 0; i < n - 1; i++)
      cout << edge[i].first + 1 << ' ' << edge[i].second + 1 << ' '
           << weis[i].first << ' ' << weis[i].second << endl;
  }
  return 0;
}
