#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
vector<int> edge[N], id[N];
long long ow[N], op[N];
long long w[N], p[N];
int a[N], b[N];
long long sum[N];
int n;
bool flag;
void dfs1(int x, int fa) {
  sum[x] = 0;
  int fid = 0;
  for (int i = 0; i <= (int)edge[x].size() - 1; ++i) {
    int y = edge[x][i];
    if (y == fa) {
      fid = id[x][i];
      continue;
    }
    dfs1(y, x);
    sum[x] += sum[y] + w[id[x][i]];
  }
  if (x == 1) return;
  long long np = max(sum[x], max(0LL, p[fid] - w[fid] + 1));
  if (np > p[fid]) flag = false;
  w[fid] += np - p[fid];
  p[fid] = np;
}
long long dfs2(int x, int fa, long long cur) {
  long long ocur = cur;
  for (int i = 0; i <= (int)edge[x].size() - 1; ++i) {
    int y = edge[x][i];
    if (y == fa) continue;
    long long inc = min(cur, ow[id[x][i]] - w[id[x][i]]);
    w[id[x][i]] += inc;
    p[id[x][i]] += inc;
    cur -= inc;
    cur -= dfs2(y, x, min(cur, p[id[x][i]] - sum[y]));
  }
  return ocur - cur;
}
int main() {
  flag = true;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    scanf("%d%d%lld%lld", a + i, b + i, w + i, p + i);
    ow[i] = w[i];
    op[i] = p[i];
    edge[a[i]].push_back(b[i]);
    id[a[i]].push_back(i);
    edge[b[i]].push_back(a[i]);
    id[b[i]].push_back(i);
  }
  dfs1(1, 0);
  if (!flag) {
    puts("-1");
    return 0;
  }
  dfs2(1, 0, 0x3f3f3f3f3f3f3f3fLL);
  printf("%d\n", n);
  for (int i = 1; i <= n - 1; ++i) {
    printf("%d %d %lld %lld\n", a[i], b[i], w[i], p[i]);
  }
  return 0;
}
