#include <bits/stdc++.h>
using namespace std;
struct Edge {
  long long x, y, w, p;
};
int n;
Edge edge[1000005], pre_edge[1000005];
long long w[1000005], ww[1000005];
vector<int> rel[1000005];
void dfs1(int x, int y, int edge_num) {
  for (int i = 0; i < rel[y].size(); i++) {
    int next = rel[y][i];
    dfs1(edge[next].x, edge[next].y, next);
  }
  if (edge_num == 0) return;
  if (edge[edge_num].p < w[y]) {
    cout << "-1" << endl;
    exit(0);
  }
  long long tmp = edge[edge_num].p - w[y];
  if (edge[edge_num].w - 1 < tmp) tmp = edge[edge_num].w - 1;
  edge[edge_num].p -= tmp;
  edge[edge_num].w -= tmp;
  w[x] += w[y] + edge[edge_num].w;
}
void dfs2(int x, int y, int edge_num, int last) {
  if (edge_num != 0) {
    int tmp = pre_edge[edge_num].p - edge[edge_num].p;
    if (last < tmp) tmp = last;
    edge[edge_num].p += tmp;
    edge[edge_num].w += tmp;
    last -= tmp;
    if (last > edge[edge_num].p - w[y]) last = edge[edge_num].p - w[y];
    ww[edge_num] = tmp;
  }
  for (int i = 0; i < rel[y].size(); i++) {
    int next = rel[y][i];
    if (edge_num == 0) last = 2e9;
    dfs2(edge[next].x, edge[next].y, next, last);
    last -= ww[next];
    ww[edge_num] += ww[next];
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> edge[i].x >> edge[i].y >> edge[i].w >> edge[i].p;
    pre_edge[i] = edge[i];
    rel[edge[i].x].push_back(i);
  }
  dfs1(0, 1, 0);
  dfs2(0, 1, 0, 2e9);
  cout << n << endl;
  for (int i = 1; i < n; i++)
    cout << edge[i].x << " " << edge[i].y << " " << edge[i].w << " "
         << edge[i].p << endl;
}
