#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
struct Edge {
  int u;
  int v;
  int w;
  int p;
  long long sumW;
};
Edge e[N];
pair<int, int> parent[N];
vector<pair<int, int> > graph[N];
int id[N];
deque<int> q[N];
bool decrease(int curE, int value, int preE) {
  e[curE].w -= value;
  e[curE].p -= value;
  int u = e[curE].u;
  if (parent[u].second != preE) {
    e[parent[u].second].sumW = -1;
  }
  if (e[curE].w == 1 || e[curE].p == e[curE].sumW) {
    return true;
  }
  return false;
}
void calSumW(int curE) {
  if (e[curE].sumW != -1) {
    return;
  }
  int u = e[curE].v;
  e[curE].sumW = 0;
  for (auto tmp : graph[u]) {
    e[curE].sumW += e[tmp.second].sumW + e[tmp.second].w;
  }
}
bool dfs(int u) {
  int curE = parent[u].second;
  id[curE] = curE;
  e[curE].sumW = 0;
  for (auto tmp : graph[u]) {
    bool ret = dfs(tmp.first);
    if (!ret) {
      return false;
    }
    int nextE = tmp.second;
    e[curE].sumW += e[nextE].w + e[nextE].sumW;
    if (q[id[curE]].size() < q[id[nextE]].size()) {
      swap(id[curE], id[nextE]);
    }
    while (!q[id[nextE]].empty()) {
      q[id[curE]].push_back(q[id[nextE]].front());
      q[id[nextE]].pop_front();
    }
    deque<int>().swap(q[id[nextE]]);
  }
  deque<int> &Q = q[id[curE]];
  if (e[curE].sumW <= e[curE].p) {
    Q.push_back(curE);
    return true;
  }
  while (!Q.empty() && e[curE].sumW > e[curE].p) {
    int nextE = Q.front();
    calSumW(nextE);
    long long value =
        min(e[curE].sumW - e[curE].p,
            min((long long)e[nextE].w - 1, e[nextE].p - e[nextE].sumW));
    bool ret = decrease(nextE, (int)value, curE);
    e[curE].sumW -= value;
    if (ret) {
      Q.pop_front();
    }
  }
  if (e[curE].sumW > e[curE].p) {
    return false;
  }
  Q.push_back(curE);
  return true;
}
int main() {
  int n, i, u, v, w, p;
  scanf("%d", &n);
  for (i = 1; i < n; i++) {
    scanf("%d%d%d%d", &u, &v, &w, &p);
    e[i] = {u, v, w, p, -1};
    graph[u].push_back({v, i});
    parent[v] = {u, i};
  }
  for (auto tmp : graph[1]) {
    int ret = dfs(tmp.first);
    if (!ret) {
      printf("-1\n");
      return 0;
    }
  }
  printf("%d\n", n);
  for (i = 1; i < n; i++) {
    printf("%d %d %d %d\n", e[i].u, e[i].v, e[i].w, e[i].p);
  }
  return 0;
}
