#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const double PI = acos(-1);
struct edge {
  int from, to, w, p;
  long long mini, maxi;
  edge(int _w, int _p) {
    w = _w;
    p = _p;
    mini = maxi = 0LL;
  }
};
const int N = 2e5 + 10;
int n;
vector<int> adjv[N], adje[N];
long long maxi[N], mini[N];
vector<edge> e;
bool init(int v, int p, int pe) {
  int i = 0;
  for (auto c : adjv[v]) {
    if (c == p) continue;
    if (adje[v][i] == pe) i++;
    int curr = adje[v][i];
    e[curr].from = v, e[curr].to = c;
    if (!init(c, v, curr)) return false;
    if (mini[c] > e[curr].p) return false;
    e[curr].mini = mini[c] + max(1LL, e[curr].w - (e[curr].p - mini[c]));
    e[curr].maxi = e[curr].w + min(0LL + e[curr].p, maxi[c]);
    mini[v] += e[curr].mini;
    maxi[v] += e[curr].maxi;
    i++;
  }
  return true;
}
void cons(int v, int p, long long rem) {
  for (auto c : adje[v]) {
    if (e[c].from == p) continue;
    mini[v] -= e[c].mini;
    maxi[v] -= e[c].maxi;
    long long spare = max(rem - e[c].maxi, mini[v]);
    long long spend = rem - spare;
    rem = spare;
    long long thispart = max(spend - e[c].w, mini[e[c].to]);
    e[c].p -= e[c].w - (spend - thispart);
    e[c].w = spend - thispart;
    cons(e[c].to, e[c].from, thispart);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < (n - 1); i++) {
    int x, y, w, p;
    cin >> x >> y >> w >> p;
    e.push_back(edge(w, p));
    adjv[x].push_back(y);
    adjv[y].push_back(x);
    adje[x].push_back(i);
    adje[y].push_back(i);
  }
  bool possible = init(1, -1, -1);
  if (!possible)
    cout << -1 << "\n";
  else {
    cons(1, -1, maxi[1]);
    cout << n << endl;
    for (auto c : e)
      cout << c.from << " " << c.to << " " << c.w << " " << c.p << endl;
  }
  return 0;
}
