#include <bits/stdc++.h>
using namespace std;
class edge {
 public:
  int x, y;
  long long w, p;
};
vector<edge> e;
vector<vector<int> > g;
vector<long long> dpmin, dpmax, dpw;
vector<long long> pe;
void dfs(int vr = 0, int p = -1) {
  for (int i : g[vr]) {
    int u = vr ^ e[i].x ^ e[i].y;
    if (p != u) {
      pe[u] = e[i].p;
      dfs(u, vr);
      dpmin[vr] += dpmin[u] + e[i].w - min(e[i].w - 1, e[i].p - dpmin[u]);
      dpmax[vr] += min(e[i].p, dpmax[u]) + e[i].w;
    }
  }
  if (dpmin[vr] > pe[vr]) {
    cout << "-1\n";
    exit(0);
  }
}
void dfs2(long long &s, int vr = 0, int p = -1) {
  for (int i : g[vr]) {
    int u = vr ^ e[i].x ^ e[i].y;
    if (p != u) {
      dpmax[u] = min(dpmax[u], e[i].p);
      s += e[i].w + dpmax[u];
    }
  }
  s -= dpmax[vr];
  for (int i : g[vr]) {
    int u = vr ^ e[i].x ^ e[i].y;
    if (p != u) {
      dfs2(s, u, vr);
      long long r = min({s, e[i].w - 1, e[i].p - dpw[u]});
      e[i].w -= r;
      e[i].p -= r;
      s -= r;
      dpw[vr] += e[i].w + dpw[u];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  g.resize(n);
  e.resize(n - 1);
  for (int i = 0; i < n - 1; i++) {
    cin >> e[i].x >> e[i].y >> e[i].w >> e[i].p;
    e[i].x--;
    e[i].y--;
    g[e[i].x].push_back(i);
    g[e[i].y].push_back(i);
  }
  dpmin.resize(n, 0);
  dpmax.resize(n, 0);
  dpw.resize(n, 0);
  pe.resize(n, 1e18);
  dfs();
  long long s = 0;
  dfs2(s);
  cout << n << "\n";
  for (int i = 0; i < n - 1; i++) {
    cout << e[i].x + 1 << " " << e[i].y + 1 << " " << e[i].w << " " << e[i].p
         << "\n";
  }
  return 0;
}
