#include <bits/stdc++.h>
using namespace std;
const int N = 200200;
int x[N], y[N], ord[N];
long long ans[N];
long long w[N], p[N];
long long sub_lo[N], sub_hi[N], wlo[N], whi[N];
long long w_final[N];
int n;
vector<int> ch[N];
bool dfs_build(int u) {
  long long ch_lo = 0, ch_hi = 0;
  for (auto& v : ch[u]) {
    bool res = dfs_build(v);
    if (!res) return false;
    ch_lo += sub_lo[v];
    ch_hi += sub_hi[v];
  }
  if (ch_lo > p[u]) return false;
  long long p_hi = p[u];
  long long p_lo = max(0LL, p[u] - (w[u]) + 1);
  long long w_hi = w[u];
  long long w_lo = w[u] + p_lo - p_hi;
  wlo[u] = w_lo;
  whi[u] = w_hi;
  sub_hi[u] = min(p_hi, ch_hi) + w_hi;
  sub_lo[u] = ch_lo + w_lo + (max(p_lo, ch_lo) - p_lo);
  assert(sub_lo[u] <= sub_hi[u]);
  return true;
}
void dfs_recon(int u, long long z) {
  long long base = wlo[u];
  for (auto& v : ch[u]) {
    base += sub_lo[v];
  }
  long long extra = z - base;
  assert(extra >= 0);
  long long take = min(extra, whi[u] - wlo[u]);
  extra -= take;
  w_final[u] = take + wlo[u];
  for (auto& v : ch[u]) {
    take = min(extra, sub_hi[v] - sub_lo[v]);
    extra -= take;
    dfs_recon(v, take + sub_lo[v]);
  }
  assert(extra == 0);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> x[i] >> y[i];
    cin >> w[y[i]] >> p[y[i]];
    ord[y[i]] = i;
    ch[x[i]].push_back(y[i]);
  }
  p[1] = 1e18;
  w[1] = 1;
  bool res = dfs_build(1);
  if (!res) {
    cout << -1 << '\n';
    return 0;
  }
  dfs_recon(1, sub_hi[1]);
  cout << n << "\n";
  for (int i = 1; i < n; ++i) {
    cout << x[i] << " " << y[i] << " " << w_final[y[i]] << " "
         << w_final[y[i]] - w[y[i]] + p[y[i]] << "\n";
  }
  return 0;
}
