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 JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.AbstractCollection;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        int n;
        ArrayList<TaskE.Rib>[] gr;
        long[] maxWeight;
        long[] minWeight;
        boolean fuck;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.readInt();
            gr = new ArrayList[n];
            for (int v = 0; v < n; v++) {
                gr[v] = new ArrayList<>(4);
            }

            long totalWeight = 0;
            for (int i = 0; i < n - 1; i++) {
                int a = in.readInt() - 1;
                int b = in.readInt() - 1;
                int w = in.readInt();
                int p = in.readInt();
                gr[a].add(new TaskE.Rib(a, b, w, p, i));
                totalWeight += w;
            }

            maxWeight = new long[n];
            minWeight = new long[n];
            fuck = false;
            f(0);
//        out.printLine("maxW:", maxWeight[0]);

            if (fuck) {
                out.printLine("-1");
                return;
            }

            TaskE.Rib[] ans = new TaskE.Rib[n - 1];
            for (ArrayList<TaskE.Rib> ribs : gr) {
                for (TaskE.Rib rib : ribs) {
                    ans[rib.id] = rib;
                }
            }

            out.printLine(n);
            for (int i = 0; i < n - 1; i++) {
                TaskE.Rib rib = ans[i];
                out.printLine(rib.v + 1, rib.to + 1, rib.w, rib.p);
            }
        }

        ArrayDeque<TaskE.Rib> f(int v) {
            ArrayList<ArrayDeque<TaskE.Rib>> ribs = new ArrayList<>();
            for (TaskE.Rib rib : gr[v]) {
                if (fuck) {
                    return new ArrayDeque<>();
                }
                ArrayDeque<TaskE.Rib> f = f(rib.to);
                f.addLast(rib);
                ribs.add(f);

                if (rib.p < minWeight[rib.to]) {
                    fuck = true;
                    return new ArrayDeque<>();
                }
                minWeight[v] += minWeight[rib.to] + rib.w - Math.min(rib.p - minWeight[rib.to], rib.w - 1);
                maxWeight[v] += rib.w + Math.min(rib.p, maxWeight[rib.to]);

                long target = maxWeight[rib.to] - rib.p;
                while (0 < target && !f.isEmpty()) {
                    TaskE.Rib r = f.getFirst();
                    long take = Math.min(Math.min(target, r.p - minWeight[r.to]), r.w - 1);
                    if (take == 0) {
                        f.pollFirst();
                    } else {
                        r.p -= take;
                        r.w -= take;
                        target -= take;
                    }
                }
            }

            ribs.sort((o1, o2) -> o2.size() - o1.size());
            if (ribs.isEmpty()) {
                return new ArrayDeque<>(0);
            }
            ArrayDeque<TaskE.Rib> ans = ribs.get(0);
            for (int i = 1; i < ribs.size(); i++) {
                ans.addAll(ribs.get(i));
            }
            return ans;
        }

        static class Rib {
            int v;
            int to;
            long w;
            long p;
            int id;

            public Rib(int v, int to, long w, long p, int id) {
                this.v = v;
                this.to = to;
                this.w = w;
                this.p = p;
                this.id = id;
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if ((c < '0') || (c > '9')) {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return (c == ' ') || (c == '\n') || (c == '\r') || (c == '\t') || (c == -1);
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}