#include <bits/stdc++.h>
using namespace std;
const int inf = 2e5 + 9;
struct nod {
  int x, y, p, w;
} que[inf];
vector<pair<int, int> > adj[inf];
int subtree[inf];
int sel_nod[inf];
int sel_list[inf];
queue<pair<int, int> > lists[inf];
int cnt = 0;
long long dfs(int x, int p, int ss) {
  sel_nod[x] = ss;
  long long sz = 0;
  int mx = -1;
  for (int i = 0; i < adj[x].size(); ++i) {
    int to = adj[x][i].first;
    if (to == p) continue;
    sz += dfs(to, x, adj[x][i].second);
    subtree[x] += subtree[to];
    if (mx == -1 || lists[sel_list[to]].size() > lists[mx].size())
      mx = sel_list[to];
  }
  sel_list[x] = mx == -1 ? cnt++ : mx;
  int select = sel_list[x];
  for (int i = 0; i < adj[x].size(); ++i) {
    int to = adj[x][i].first;
    if (to == p) continue;
    int tmp = sel_list[to];
    if (tmp == select) continue;
    while (!lists[tmp].empty()) {
      lists[select].push(lists[tmp].front());
      lists[tmp].pop();
    }
  }
  if (x != 1) {
    int power = que[sel_nod[x]].p;
    while (sz > power && !lists[select].empty()) {
      int tmp = lists[select].front().first;
      int e = lists[select].front().second;
      long long k =
          min(sz - power, (long long)min(que[tmp].p - e, que[tmp].w - 1));
      if (k > 0) {
        sz -= k;
        que[tmp].p -= k;
        que[tmp].w -= k;
      }
      if (que[tmp].w <= 1 || que[tmp].p <= e) lists[select].pop();
    }
    if (sz > power) {
      printf("-1\n");
      exit(0);
    }
  }
  lists[select].push(pair<int, int>(sel_nod[x], subtree[x]));
  subtree[x] += max(que[sel_nod[x]].w - que[sel_nod[x]].p + subtree[x], 1);
  return sz + que[sel_nod[x]].w;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d%d%d%d", &que[i].x, &que[i].y, &que[i].w, &que[i].p);
    adj[que[i].x].push_back(pair<int, int>(que[i].y, i));
    adj[que[i].y].push_back(pair<int, int>(que[i].x, i));
  }
  dfs(1, -1, 0);
  printf("%d\n", n);
  for (int i = 0; i < n - 1; ++i)
    printf("%d %d %d %d\n", que[i].x, que[i].y, que[i].w, que[i].p);
}
