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:
/**
 * BaZ :D
 */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class ACMIND
{
    static FastReader scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 1_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null,null,"BaZ",1<<25)
        {
            public void run()
            {
                try
                {
                    solve();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }
    static int leaf_in_subtree[], par[];
    static ArrayList<Pair> adj[];
    static ArrayList<Integer> ansl1, ansl2,ans_val;
    static void solve() throws IOException
    {
        scan = new FastReader();
        pw = new PrintWriter(System.out,true);
        StringBuilder sb = new StringBuilder();
        int n = ni();
        adj = new ArrayList[n+1];
        for(int i=1;i<=n;++i) {
            adj[i] = new ArrayList<>();
        }
        for(int i=1;i<n;++i) {
            int u = ni(), v = ni(), w = ni();
            adj[u].add(new Pair(v,w));
            adj[v].add(new Pair(u,w));
        }
        int root = -1;
        for(int i=1;i<=n;++i) {
            if(adj[i].size()==2) {
                root = -1;
                break;
            }
            else if(adj[i].size()>1) {
                root = i;
            }
        }
        par = new int[n+1];
        if(root==-1) {
            if(n==2) {

                pl("YES\n1\n1 2 "+adj[1].get(0).y);
            }
            else {
                pl("NO");
            }
            System.exit(0);
        }
        ansl1 = new ArrayList<>();
        ansl2 = new ArrayList<>();
        ans_val = new ArrayList<>();
        leaf_in_subtree = new int[n+1];
        dfs1(root,root);
//        pl("root: "+root);
//        pa("leaf in subtree", leaf_in_subtree);
        dfs2(root, root);
        sb.append("YES\n");
        sb.append(ansl1.size());
        sb.append("\n");
        int sz = ansl1.size();
        for(int i=0;i<sz;++i) {
            sb.append(ansl1.get(i));
            sb.append(" ");
            sb.append(ansl2.get(i));
            sb.append(" ");
            sb.append(ans_val.get(i));
            sb.append("\n");
        }
        psb(sb);
        pw.flush();
        pw.close();
    }

    static void dfs1(int v, int baap) {
        par[v] = baap;
        int cc = 0;
        for(Pair e : adj[v]) {
            if(e.x!=baap) {
                dfs1(e.x, v);
                leaf_in_subtree[v] = leaf_in_subtree[e.x];
                cc++;
            }
        }
        if(cc==0) {
            leaf_in_subtree[v] = v;
        }
    }

    static void dfs2(int v, int baap) {
        for(Pair p : adj[v]) {
            if(p.x!=baap) {
                add(v, leaf_in_subtree[p.x], p.y);
                if(p.x!=leaf_in_subtree[p.x]) {
                    add(p.x, leaf_in_subtree[p.x], -p.y);
                }
                dfs2(p.x, v);
            }
        }
    }

    static void add(int v, int l, int val) {
        if(v==l) {
            return;
        }
        Pair p = get_two_leaves_other_than_l(v, l);
        ansl1.add(p.x);
        ansl2.add(l);
        ans_val.add(val/2);

        ansl1.add(p.y);
        ansl2.add(l);
        ans_val.add(val/2);

        ansl1.add(p.x);
        ansl2.add(p.y);
        ans_val.add((-val/2));
    }

    static Pair get_two_leaves_other_than_l(int v, int l) {
        int l1 = -1;
        for(Pair e : adj[v]) {
            if(e.x!=par[v]) {
                if(leaf_in_subtree[e.x]!=l) {
                    if(l1==-1) {
                        l1 = leaf_in_subtree[e.x];
                    }
                    else {
                        return new Pair(l1, leaf_in_subtree[e.x]);
                    }
                }
            }
        }
        for(Pair e : adj[par[v]]) {
            if(e.x!=v && e.x!=par[par[v]]) {
                return new Pair(l1, leaf_in_subtree[e.x]);
            }
        }
        return new Pair(l1, -1);
    }
    static class Pair implements Comparable<Pair>
    {
        int x,y;
        Pair(int x,int y)
        {
            this.x=x;
            this.y=y;
        }
        public int compareTo(Pair other)
        {
            if(this.x!=other.x)
                return this.x-other.x;
            return this.y-other.y;
        }
        public String toString()
        {
            return "("+x+","+y+")";
        }
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static class FastReader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public FastReader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public FastReader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[1000000];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
            if (neg) return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null) return;
            din.close();
        }
    }
}