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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;




public class  gym{
static class pair implements Comparable<pair>{
	int num;int idx;
	pair(int x,int y){
		num=x;idx=y;
	}
	@Override
	public int compareTo(pair o) {
		if(num!=o.num) {
			return num-o.num;
		}
		return idx-o.idx;
	}
	 
	public boolean equals(pair o) {
		if(this.compareTo(o)==0)return true;
		return false;
	}
	public String toString() {
		return "("+0+" "+0+")";
	}
}
static class Triple implements Comparable<Triple>
{
	int i,j,cost;
	Triple(int x, int y, int z){i = x; j = y; cost = z;}
	
	public int compareTo(Triple t) {
		return this.cost - t.cost;
	}
	public String toString() {
		return i+" "+j+" "+cost;
	}
	
}
	static boolean[]v;
	static int[]leaves;
	static int c;
	static LinkedList<Integer>[]adj;
	static void dfs(int i,int j,int num) {
		v[i]=true;
		if(num<=0)return;
		if(adj[i].size()==1) {
			if(c<2)
				leaves[c++]=i;
			return;
		}
		for(int o:adj[i]) {
			if(o!=j && !v[o]) {
				if(num==2) {
					dfs(o,j,1);
				}
				else {
					dfs(o,j,num--);
				}
			}
		}
	}
    public static void main(String[] args) throws IOException{
        MScanner sc = new MScanner(System.in);
        PrintWriter pw=new PrintWriter(System.out);
        
        int n = sc.nextInt();
        adj=new LinkedList[n];
        for(int i=0;i<n;i++)adj[i]=new LinkedList<Integer>();
        Triple []edges=new Triple[n-1];
        for(int i=0;i<n-1;i++) {
        	int x=sc.nextInt()-1;int y=sc.nextInt()-1;int val=sc.nextInt();
        	edges[i]=new Triple(x, y,val);
        	adj[x].add(y);adj[y].add(x);
        }
        for(int i=0;i<n;i++) {
        	if(adj[i].size()==2) {
        		pw.println("NO");pw.flush();return;
        	}
        }
        pw.println("YES");
        StringBuilder ans=new StringBuilder();int count=0;
        for(int i=0;i<n-1;i++) {
        	int x=edges[i].i;int y=edges[i].j;int val=edges[i].cost;
        	//System.err.println(x+" "+y+" "+val);
        	if(val==0)continue;
        	if(adj[x].size()==1 || adj[y].size()==1) {
        		if(adj[x].size()==1 && adj[y].size()==1) {
        			ans.append((x+1)+" "+(y+1)+" "+(val));ans.append('\n');
        			count++;
        			continue;
        		}
        		if(adj[x].size()==1) {
        			v=new boolean[n];
                	leaves=new int[2];c=0;
                	dfs(y,x,2);
                	//System.out.println(c);
                	int n1=leaves[0]+1;int n2=leaves[1]+1;
                	ans.append((x+1)+" "+n1+" "+(val/2));ans.append('\n');
                	ans.append((x+1)+" "+n2+" "+(val/2));ans.append('\n');
                	ans.append(n1+" "+n2+" "+(-val/2));ans.append('\n');
        		}
        		else {
        			v=new boolean[n];
                	leaves=new int[2];c=0;
                	dfs(x,y,2);
                	//System.out.println(c);
                	int n1=leaves[0]+1;int n2=leaves[1]+1;
                	ans.append((y+1)+" "+n1+" "+(val/2));ans.append('\n');
                	ans.append((y+1)+" "+n2+" "+(val/2));ans.append('\n');
                	ans.append(n1+" "+n2+" "+(-val/2));ans.append('\n');
        		}
        		count+=3;
        		continue;
        	}
        	v=new boolean[n];
        	leaves=new int[2];c=0;
        	dfs(x,y,2);
        	//System.out.println(c);
        	int n1=leaves[0]+1;int n2=leaves[1]+1;
        	v=new boolean[n];
        	leaves=new int[2];c=0;
        	dfs(y,x,2);
        	//System.out.println(c);
        	int n3=leaves[0]+1;int n4=leaves[1]+1;
        	ans.append(n1+" "+n4+" "+(val/2));ans.append('\n');
        	ans.append(n2+" "+n3+" "+(val/2));ans.append('\n');
        	ans.append(n1+" "+n2+" "+(-val/2));ans.append('\n');
        	ans.append(n3+" "+n4+" "+(-val/2));ans.append('\n');
        	count+=4;
        }
        pw.println(count);
        pw.print(ans);
        pw.flush();
    }
    static class Vector {

    	double x, y; 

    	Vector(double a, double b) { x = a; y = b; }

    	Vector(Point a, Point b) { this(b.x - a.x, b.y - a.y); }

    	Vector scale(double s) { return new Vector(x * s, y * s); }              //s is a non-negative value

    	double dot(Vector v) { return (x * v.x + y * v.y); }

    	double cross(Vector v) { return x * v.y - y * v.x; }

    	double norm2() { return x * x + y * y; }

    	Vector reverse() { return new Vector(-x, -y); }

    	Vector normalize() 
    	{ 
    		double d = Math.sqrt(norm2());
    		return scale(1 / d);
    	}		
    }
    static class Point implements Comparable<Point>{

    	static final double EPS = 1e-9;

    	double x, y;                  

    	Point(double a, double b) { x = a; y = b; }  
    	
    	public int compareTo(Point p)
    	{
    		if(Math.abs(x - p.x) > EPS) return x > p.x ? 1 : -1;
    		if(Math.abs(y - p.y) > EPS) return y > p.y ? 1 : -1;
    		return 0;
    	}
    	public String toString() {
    		return "("+x+" "+y+")";
    	}
    	public double dist(Point p) { return sq(x - p.x) + sq(y - p.y); }
    	
    	static double sq(double x) { return x * x; }
    	
    	Point rotate(double angle)
    	{
    		double c = Math.cos(angle), s = Math.sin(angle);
    		return new Point(x * c - y * s, x * s + y * c);
    	}
    	// for integer points and rotation by 90 (counterclockwise) : swap x and y, negate x
    	
    	Point rotate(double theta, Point p)			//rotate around p
    	{
    		Vector v = new Vector(p, new Point(0, 0));
    		return translate(v).rotate(theta).translate(v.reverse());
    	}
    	
    	Point translate(Vector v) { return new Point(x + v.x , y + v.y); }
    	
    	Point reflectionPoint(Line l) 	//reflection point of p on line l
    	{
    		Point p = l.closestPoint(this);
    		Vector v = new Vector(this, p);
    		return this.translate(v).translate(v);
    	}
    	
    	boolean between(Point p, Point q)
    	{
    		return x < Math.max(p.x, q.x) + EPS && x + EPS > Math.min(p.x, q.x)
    				&& y < Math.max(p.y, q.y) + EPS && y + EPS > Math.min(p.y, q.y);
    	}
    	
    	//returns true if it is on the line defined by a and b
    	boolean onLine(Point a, Point b) 
    	{
    		if(a.compareTo(b) == 0) return compareTo(a) == 0;
    		return Math.abs(new Vector(a, b).cross(new Vector(a, this))) < EPS;
    	}
    	
    	boolean onSegment(Point a, Point b)
    	{
    		if(a.compareTo(b) == 0) return compareTo(a) == 0;
    		return onRay(a, b) && onRay(b, a);
    	}
    	
    	//returns true if it is on the ray whose start point is a and passes through b
    	boolean onRay(Point a, Point b)
    	{
    		if(a.compareTo(b) == 0) return compareTo(a) == 0;
    		return new Vector(a, b).normalize().equals(new Vector(a, this).normalize());	//implement equals()
    	}
    	
    	// returns true if it is on the left side of Line pq
    	// add EPS to LHS if on-line points are accepted
    	static boolean ccw(Point p, Point q, Point r)
    	{
    		return new Vector(p, q).cross(new Vector(p, r)) > 0;
    	}
    	
    	static boolean collinear(Point p, Point q, Point r)
    	{
    		return Math.abs(new Vector(p, q).cross(new Vector(p, r))) < EPS;
    	}
    	
    	static double angle(Point a, Point o, Point b)  // angle AOB
    	{
    		Vector oa = new Vector(o, a), ob = new Vector(o, b);
    		return Math.acos(oa.dot(ob) / Math.sqrt(oa.norm2() * ob.norm2()));
    	}
    	
    	static double distToLine(Point p, Point a, Point b) //distance between point p and a line defined by points a, b (a != b)
    	{
    		if(a.compareTo(b) == 0) return p.dist(a);
    		// formula: c = a + u * ab
    		Vector ap = new Vector(a, p), ab = new Vector(a, b);
    		double u = ap.dot(ab) / ab.norm2();
    		Point c = a.translate(ab.scale(u)); 
    		return p.dist(c);
    	}
    	// Another way: find closest point and calculate the distance between it and p

    	static double distToLineSegment(Point p, Point a, Point b) 
    	{
    		Vector ap = new Vector(a, p), ab = new Vector(a, b);
    		double u = ap.dot(ab) / ab.norm2();
    		if (u < 0.0) return p.dist(a);
    		if (u > 1.0) return p.dist(b);        
    		return distToLine(p, a, b); 
    	}
    	// Another way: find closest point and calculate the distance between it and p
    }
    static class Line {

    	static final double INF = 1e9, EPS = 1e-9;
    	
    	double a, b, c;
    	
    	Line(Point p, Point q)
    	{
    		if(Math.abs(p.x - q.x) < EPS) {	a = 1; b = 0; c = -p.x;	}
    		else
    		{
    			a = (p.y - q.y) / (q.x - p.x);
    			b = 1.0;
    			c = -(a * p.x + p.y);
    		}
    					
    	}
    	
    	Line(Point p, double m) { a = -m; b = 1; c =  -(a * p.x + p.y); } 
    	
    	boolean parallel(Line l) { return Math.abs(a - l.a) < EPS && Math.abs(b - l.b) < EPS; }
    	
    	boolean same(Line l) { return parallel(l) && Math.abs(c - l.c) < EPS; }
    	
    	Point intersect(Line l)
    	{
    		if(parallel(l))
    			return null;
    		double x = (b * l.c - c * l.b) / (a * l.b - b * l.a);
    		double y;
    		if(Math.abs(b) < EPS)
    			 y = -l.a * x - l.c;
    		else
    			y = -a * x - c;
    		
    		return new Point(x, y);
    	}
    	
    	Point closestPoint(Point p)
    	{
    		if(Math.abs(b) < EPS) return new Point(-c, p.y);
    		if(Math.abs(a) < EPS) return new Point(p.x, -c);
    		return intersect(new Line(p, 1 / a));
    	}
    			
    }
    static long gcd(long a, long b) {

		if (b == 0)
			return a;
		return gcd(b, a % b);
	}
    static int[]primes;
    static int sizeofp=0;
	static int[] isComposite;

	static void sieve(int N)	// O(N log log N) 
	{
		isComposite = new int[N+1];					
		isComposite[0] = isComposite[1] = 1;			
		primes = new int[N];

		for (int i = 2; i <= N; ++i) 					
			if (isComposite[i] == 0) 					
			{
				primes[sizeofp++]=i;;
				if(1l * i * i <= N)
					for (int j = i * i; j <= N; j += i)	
						isComposite[j] = 1;
			}   
	}
    
	static class MScanner 
	{
	    StringTokenizer st;
	    BufferedReader br;

	    public MScanner(InputStream s){    br = new BufferedReader(new InputStreamReader(s));}

	    public String next() throws IOException 
	    {
	        while (st == null || !st.hasMoreTokens()) 
	            st = new StringTokenizer(br.readLine());
	        return st.nextToken();
	    }

	    public int nextInt() throws IOException {return Integer.parseInt(next());}
	    
	    public long nextLong() throws IOException {return Long.parseLong(next());}

	    public String nextLine() throws IOException {return br.readLine();}
	    
	    public double nextDouble() throws IOException
	    {
	        String x = next();
	        StringBuilder sb = new StringBuilder("0");
	        double res = 0, f = 1;
	        boolean dec = false, neg = false;
	        int start = 0;
	        if(x.charAt(0) == '-')
	        {
	            neg = true;
	            start++;
	        }
	        for(int i = start; i < x.length(); i++)
	            if(x.charAt(i) == '.')
	            {
	                res = Long.parseLong(sb.toString());
	                sb = new StringBuilder("0");
	                dec = true;
	            }
	            else
	            {
	                sb.append(x.charAt(i));
	                if(dec)
	                    f *= 10;
	            }
	        res += Long.parseLong(sb.toString()) / f;
	        return res * (neg?-1:1);
	    }
	    
	    public boolean ready() throws IOException {return br.ready();}


	}
}