Problem: Note that this is the second problem of the two similar problems. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.

You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any integer number x and add x to values written on all edges on the simple path between u and v. Note that in previous subtask x was allowed to be any real, here it has to be integer.

For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -1 on the path from 4 to 5. 

<image>

You are given some configuration of nonnegative integer pairwise different even numbers, written on the edges. For a given configuration determine if it is possible to achieve it with these operations, and, if it is possible, output the sequence of operations that leads to the given configuration. Constraints on the operations are listed in the output format section.

Leave is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.

Input

The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of nodes in a tree.

Each of the next n-1 lines contains three integers u, v, val (1 ≤ u, v ≤ n, u ≠ v, 0 ≤ val ≤ 10 000), meaning that there is an edge between nodes u and v with val written on it. It is guaranteed that these edges form a tree. It is guaranteed that all val numbers are pairwise different and even. 

Output

If there aren't any sequences of operations which lead to the given configuration, output "NO".

If it exists, output "YES" in the first line. In the second line output m — number of operations you are going to apply (0 ≤ m ≤ 10^5). Note that you don't have to minimize the number of the operations!

In the next m lines output the operations in the following format:

u, v, x (1 ≤ u, v ≤ n, u not = v, x — integer, -10^9 ≤ x ≤ 10^9), where u, v — leaves, x — number we are adding. 

It is guaranteed that if there exists a sequence of operations producing given configuration, then there exists a sequence of operations producing given configuration, satisfying all the conditions above.

Examples

Input


5
1 2 2
2 3 4
3 4 10
3 5 18


Output


NO

Input


6
1 2 6
1 3 8
1 4 12
2 5 2
2 6 4


Output


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

Note

The configuration from the first sample is drawn below, and it is impossible to achieve.

<image>

The sequence of operations from the second sample is illustrated below.

<image>

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.InputStreamReader;
import java.util.ArrayList;
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;
        MyInput in = new MyInput(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        A2AddOnATreeRevolution solver = new A2AddOnATreeRevolution();
        solver.solve(1, in, out);
        out.close();
    }

    static class A2AddOnATreeRevolution {
        List<int[]> operation = new ArrayList<>();
        int[] w;
        List<Edge>[] g;

        public void solve(int testNumber, MyInput in, PrintWriter out) {
            int n = in.nextInt();
            g = new List[n];
            w = new int[n - 1];
            for (int i = 0; i < n; i++) {
                g[i] = new ArrayList<>();
            }
            for (int i = 0; i < n - 1; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt() - 1;
                int we = in.nextInt();
                w[i] = we;
                g[a].add(new Edge(b, i));
                g[b].add(new Edge(a, i));
            }

            if (n == 2) {
                out.println("YES");
                out.println("1");
                out.println("1 2 " + w[0]);
                return;
            }

            final List<Integer> leafs = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if (g[i].size() == 2) {
                    out.println("NO");
                    return;
                }
                if (g[i].size() == 1) {
                    leafs.add(i);
                }
            }

            for (int ii = 0; ii + 1 < leafs.size(); ii++) {
                int i = leafs.get(ii);
                int j = leafs.get(ii + 1);
                operation.add(new int[]{i, j, w[g[i].get(0).ei]});
                dfs(i, -1, j, w[g[i].get(0).ei]);
            }
            {
                int i = leafs.get(leafs.size() - 1);
                int j = leafs.get(0);
                int l = leafs.get(1);
                int val = w[g[i].get(0).ei] / 2;
                operation.add(new int[]{i, j, val});
                operation.add(new int[]{i, l, val});
                operation.add(new int[]{l, j, -val});
                dfs(i, -1, j, val);
                dfs(i, -1, l, val);
                dfs(j, -1, l, -val);
            }

            int[] t1 = new int[2];
            int[] t2 = new int[2];
            for (int v = 0; v < n; v++) {
                for (Edge e : g[v]) {
                    if (w[e.ei] == 0) continue;
                    Arrays.fill(t1, -1);
                    Arrays.fill(t2, -1);
                    dfs(v, e.to, t1);
                    dfs(e.to, v, t2);
                    operation.add(new int[]{t1[0], t2[0], w[e.ei] / 2});
                    operation.add(new int[]{t1[1], t2[1], w[e.ei] / 2});
                    operation.add(new int[]{t1[0], t1[1], -w[e.ei] / 2});
                    operation.add(new int[]{t2[0], t2[1], -w[e.ei] / 2});
                    w[e.ei] = 0;
                }
            }
            out.println("YES");
            out.println(operation.size());
            for (int[] op : operation) {
                out.println((op[0] + 1) + " " + (op[1] + 1) + " " + op[2]);
            }
        }

        void dfs(int v, int p, int[] tmp) {
            if (g[v].size() == 1) {
                tmp[tmp[0] >= 0 ? 1 : 0] = v;
                return;
            }
            for (Edge e : g[v]) {
                int i = tmp[0] < 0 ? 0 : 1;
                if (e.to != p) dfs(e.to, v, tmp);
                if (tmp[i] >= 0 && i == 1) return;
                if (tmp[i] >= 0) tmp[i + 1] = -1;
            }
        }

        boolean dfs(int v, int p, int to, int dw) {
            if (v == to) return true;
            for (Edge e : g[v]) {
                if (e.to != p && dfs(e.to, v, to, dw)) {
                    w[e.ei] -= dw;
                    return true;
                }
            }
            return false;
        }

        class Edge {
            final int to;
            final int ei;

            public Edge(int b, int i) {
                to = b;
                ei = i;
            }

        }

    }

    static class MyInput {
        private final BufferedReader in;
        private static int pos;
        private static int readLen;
        private static final char[] buffer = new char[1024 * 8];
        private static char[] str = new char[500 * 8 * 2];
        private static boolean[] isDigit = new boolean[256];
        private static boolean[] isSpace = new boolean[256];
        private static boolean[] isLineSep = new boolean[256];

        static {
            for (int i = 0; i < 10; i++) {
                isDigit['0' + i] = true;
            }
            isDigit['-'] = true;
            isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
            isLineSep['\r'] = isLineSep['\n'] = true;
        }

        public MyInput(InputStream is) {
            in = new BufferedReader(new InputStreamReader(is));
        }

        public int read() {
            if (pos >= readLen) {
                pos = 0;
                try {
                    readLen = in.read(buffer);
                } catch (IOException e) {
                    throw new RuntimeException();
                }
                if (readLen <= 0) {
                    throw new MyInput.EndOfFileRuntimeException();
                }
            }
            return buffer[pos++];
        }

        public int nextInt() {
            int len = 0;
            str[len++] = nextChar();
            len = reads(len, isSpace);
            int i = 0;
            int ret = 0;
            if (str[0] == '-') {
                i = 1;
            }
            for (; i < len; i++) ret = ret * 10 + str[i] - '0';
            if (str[0] == '-') {
                ret = -ret;
            }
            return ret;
        }

        public char nextChar() {
            while (true) {
                final int c = read();
                if (!isSpace[c]) {
                    return (char) c;
                }
            }
        }

        int reads(int len, boolean[] accept) {
            try {
                while (true) {
                    final int c = read();
                    if (accept[c]) {
                        break;
                    }
                    if (str.length == len) {
                        char[] rep = new char[str.length * 3 / 2];
                        System.arraycopy(str, 0, rep, 0, str.length);
                        str = rep;
                    }
                    str[len++] = (char) c;
                }
            } catch (MyInput.EndOfFileRuntimeException e) {
            }
            return len;
        }

        static class EndOfFileRuntimeException extends RuntimeException {
        }

    }
}