Problem: You are given a tree that has n vertices, which are numbered from 1 to n, where the vertex number one is the root. Each edge has weight wi and strength pi.

Botanist Innokentiy, who is the only member of the jury of the Olympiad in Informatics, doesn't like broken trees. 

The tree is broken if there is such an edge the strength of which is less than the sum of weight of subtree's edges to which it leads.

It is allowed to reduce weight of any edge by arbitrary integer value, but then the strength of its edge is reduced by the same value. It means if the weight of the edge is 10, and the strength is 12, then by the reducing the weight by 7 its weight will equal 3, and the strength will equal 5. 

It is not allowed to increase the weight of the edge.

Your task is to get the tree, which is not broken, by reducing the weight of edges of the given tree, and also all edged should have the positive weight, moreover, the total weight of all edges should be as large as possible.

It is obvious that the strength of edges can not be negative, however it can equal zero if the weight of the subtree equals zero. 

Input

The first line contains the integer n (1 ≤ n ≤ 2·105) — the number of vertices in the tree. The next n - 1 lines contains the description of edges. Each line contains four integers x, y, w, p (1 ≤ x, y ≤ n, 1 ≤ w ≤ 109, 0 ≤ p ≤ 109), where x and y — vertices which connect the edge (the vertex number x is the parent of the vertex number y), w and p are the weight and the strength of the edge, accordingly. It is guaranteed that the edges describe the tree with the root in the vertex 1.

Output

If it is impossible to get unbroken tree from the given tree, print -1 in the only line.

Otherwise, the output data should contain n lines:

In the first line print the number n — the number of vertices on the tree. 

In the next n - 1 lines print the description of edges of the resulting tree. Each line should contain four integers x, y, w, p (1 ≤ x, y ≤ n, 1 ≤ w ≤ 109, 0 ≤ p ≤ 109), where x and y — vertices, which the edge connects (the vertex number x is the parent of the vertex number y), w and p are the new weight and the strength of the edge, accordingly. 

Print edges in the same order as they are given in input data: the first two integers of each line should not be changed. 

Examples

Input

3
1 3 5 7
3 2 4 3


Output

3
1 3 5 7
3 2 4 3


Input

4
1 3 2 3
3 4 5 1
3 2 3 3


Output

-1

Input

5
1 2 2 4
2 4 1 9
4 5 5 6
4 3 4 8


Output

5
1 2 2 4
2 4 1 9
4 5 1 2
4 3 2 6


Input

7
1 2 5 2
2 3 4 3
1 4 3 7
4 5 4 1
4 6 3 2
6 7 1 6


Output

7
1 2 5 2
2 3 2 1
1 4 3 7
4 5 3 0
4 6 3 2
6 7 1 6

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void minimize(X &x, const Y &y) {
  if (x > y) x = y;
}
template <class X, class Y>
void maximize(X &x, const Y &y) {
  if (x < y) x = y;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
class FenwickTree {
 private:
  vector<long long> v;
  int n;
  long long get(int x) const {
    long long res = 0;
    for (; x >= 1; x &= x - 1) res += v[x];
    return res;
  }

 public:
  FenwickTree(int n = 0) {
    this->n = n;
    v.assign(n + 7, 0);
  }
  void update(int x, long long d) {
    for (; x <= n; x += x & -x) v[x] += d;
  }
  long long get(int l, int r) const {
    return l == 1 ? get(r) : get(r) - get(l - 1);
  }
};
class SegmentTree {
 private:
  vector<int> tree;
  int n;
  void update(int i, int l, int r, int pos, int val) {
    if (l == r) {
      tree[i] = val;
      return;
    }
    int m = (l + r) >> 1;
    if (pos > m)
      update(2 * i + 1, m + 1, r, pos, val);
    else
      update(2 * i, l, m, pos, val);
    tree[i] = tree[2 * i + 1] < 0 ? tree[2 * i] : tree[2 * i + 1];
  }
  int get(int i, int l, int r, int u, int v) const {
    if (l > v || r < u || l > r || v < u) return -1;
    if (u <= l && r <= v) return tree[i];
    int m = (l + r) >> 1;
    int R = get(2 * i + 1, m + 1, r, u, v);
    if (R >= 0) return R;
    return get(2 * i, l, m, u, v);
  }
  void debug(int i, int l, int r) const {
    printf("NODE %d (%d - %d): %d\n", i, l, r, tree[i]);
    if (l == r) return;
    int m = (l + r) >> 1;
    debug(2 * i, l, m);
    debug(2 * i + 1, m + 1, r);
  }

 public:
  SegmentTree(int n = 0) {
    this->n = n;
    tree.assign(4 * n + 7, -1);
  }
  void insert(int pos, int val) { update(1, 1, n, pos, val); }
  void remove(int pos) { update(1, 1, n, pos, -1); }
  int get(int l, int r) const { return get(1, 1, n, l, r); }
  void debug(void) const { debug(1, 1, n); }
};
const int MIN_W = 1;
const int MIN_S = 0;
struct Edge {
  int par, child, weight, strength, delta;
  Edge(int p = 0, int c = 0, int w = 0, int s = 0) {
    par = p;
    child = c;
    weight = w;
    strength = s;
  }
  void reduce(int delta) {
    weight -= delta;
    strength -= delta;
    assert(weight >= MIN_W && strength >= MIN_S);
  }
  int canChange(void) const { return min(weight - MIN_W, strength - MIN_S); }
  void printEdge(void) const {
    printf("%d %d %d %d\n", par, child, weight, strength);
  }
};
Edge edges[200200];
vector<int> children[200200];
int n, cnt, sta[200200], fin[200200], parEdge[200200];
void loadTree(void) {
  scanf("%d", &n);
  for (int i = 0, _n = (n - 1); i < _n; i = i + 1) {
    int p, c, w, s;
    scanf("%d%d%d%d", &p, &c, &w, &s);
    edges[i] = Edge(p, c, w, s);
    parEdge[c] = i;
    children[p].push_back(i);
  }
}
void dfs(int u) {
  sta[u] = ++cnt;
  for (__typeof((children[u]).begin()) it = (children[u]).begin();
       it != (children[u]).end(); it++)
    dfs(edges[*it].child);
  fin[u] = cnt;
}
bool compare(const int &x, const int &y) { return sta[x] > sta[y]; }
void noAnswer(void) {
  printf("-1\n");
  exit(0);
}
void process(void) {
  dfs(1);
  vector<int> nodes;
  for (int i = (1), _b = (n); i <= _b; i = i + 1) nodes.push_back(i);
  sort((nodes).begin(), (nodes).end(), compare);
  FenwickTree bit(n);
  SegmentTree myit(n);
  for (int i = 0, _n = (n - 1); i < _n; i = i + 1)
    bit.update(sta[edges[i].child], edges[i].weight);
  for (int i = 0, _n = (n - 1); i < _n; i = i + 1)
    myit.insert(sta[edges[i].child], i);
  for (__typeof((nodes).begin()) it = (nodes).begin(); it != (nodes).end();
       it++)
    if (*it != 1) {
      int id = parEdge[*it];
      long long needChange =
          bit.get(sta[*it] + 1, fin[*it]) - edges[id].strength;
      while (needChange > 0) {
        int recEdge = myit.get(sta[*it] + 1, fin[*it]);
        if (recEdge < 0) {
          noAnswer();
        }
        int recChild = edges[recEdge].child;
        int change =
            edges[recEdge].strength - bit.get(sta[recChild] + 1, fin[recChild]);
        minimize(change, edges[recEdge].canChange());
        minimize(change, needChange);
        if (change == 0) {
          myit.remove(sta[recChild]);
          continue;
        }
        edges[recEdge].reduce(change);
        bit.update(sta[recChild], -change);
        needChange -= change;
      }
    }
  printf("%d\n", n);
  for (int i = 0, _n = (n - 1); i < _n; i = i + 1) edges[i].printEdge();
}
int main(void) {
  loadTree();
  process();
  return 0;
}