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

Solution:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T>
void uin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}
template <typename T>
void uax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}
const long long N = 1 * 1000 + 228;
struct oper {
  long long u, v, c;
  oper() {}
  oper(long long _u, long long _v, long long _c) { u = _u, v = _v, c = _c; }
};
long long deg[N];
vector<pair<long long, long long> > g[N], gr[N];
long long leaf[N][N];
long long st = 0;
vector<oper> ans;
long long jfs(long long v, long long par = -1) {
  for (pair<long long, long long> go : g[v]) {
    long long to = go.first;
    if (to != par) {
      leaf[st][v] = jfs(to, v);
    }
  }
  if (deg[v] == 1) {
    leaf[st][v] = v;
  }
  return leaf[st][v];
}
long long kek(long long v, long long par = -1) {
  if (deg[v] != 2) return v;
  for (pair<long long, long long> go : g[v]) {
    long long to = go.first;
    if (to == par) continue;
    return kek(to, v);
  }
  return -1;
}
long long cost[N], p[N];
void dfs(long long v, long long par = -1) {
  p[v] = par;
  for (pair<long long, long long> go : g[v]) {
    long long to = go.first;
    if (to == par) continue;
    dfs(to, v);
  }
}
bool cmp(long long i, long long j) { return g[i].size() < g[j].size(); }
long long start = 0;
bool dfs_add(long long v, long long t, long long add, long long par = -1) {
  if (v == t) {
    return 1;
  }
  for (pair<long long, long long> go : g[v]) {
    long long to = go.first, en = go.second;
    if (to == par) continue;
    bool iff = dfs_add(to, t, add, v);
    if (iff) {
      cost[en] += add;
      return 1;
    }
  }
  return 0;
}
void apply(long long a, long long b, long long c) {
  if (a == b) return;
  ans.push_back(oper(a, b, -c));
  start = a;
  dfs_add(a, b, c);
}
void solve(long long v, long long pe = 0, long long par = -1) {
  if ((long long)g[v].size() == 1) return;
  vector<pair<long long, long long> > sons;
  for (auto go : g[v]) {
    if (go.first == par) continue;
    sons.push_back(go);
  }
  pair<long long, long long> extra = make_pair(0, 0);
  if ((long long)sons.size() == 2) {
    extra = make_pair(par, pe);
  } else {
    extra = sons[0];
  }
  long long sz = (long long)sons.size();
  for (long long i = 0; i < (long long)sons.size() - 1; ++i) {
    pair<long long, long long> p1 = sons[i], p2 = sons[i + 1];
    apply(leaf[v][p1.first], leaf[v][p2.first], -cost[p1.second]);
  }
  pair<long long, long long> p1 = sons[sz - 2], p2 = sons[sz - 1];
  long long C = cost[p2.second];
  apply(leaf[v][p1.first], leaf[v][extra.first], C / 2);
  apply(leaf[v][p2.first], leaf[v][extra.first], -C / 2);
  apply(leaf[v][p1.first], leaf[v][p2.first], -C / 2);
  for (auto go : g[v]) {
    long long to = go.first;
    if (to == par) continue;
    solve(to, go.second, v);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n - 1; ++i) {
    long long u, v, c;
    cin >> u >> v >> c;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
    ++deg[u];
    ++deg[v];
  }
  if (n == 2) {
    cout << "YES\n";
    cout << 1 << endl;
    cout << 1 << " " << 2 << " " << g[1][0].second << '\n';
    return 0;
  }
  for (long long i = 1; i <= n; ++i) {
    st = i;
    jfs(i);
  }
  for (long long i = 1; i <= n; ++i) {
    if (deg[i] == 2) {
      if (g[i][0].second != g[i][1].second) {
        cout << "NO\n";
        return 0;
      }
    }
  }
  long long Ptr = 0;
  map<pair<long long, long long>, bool> used;
  vector<long long> oks;
  for (long long i = 1; i <= n; ++i) {
    if (deg[i] == 2) continue;
    oks.push_back(i);
    for (pair<long long, long long> go : g[i]) {
      long long to = go.first;
      long long TO = kek(to, i);
      if (used[{i, TO}]) continue;
      cost[Ptr] = go.second;
      gr[i].push_back({TO, Ptr});
      gr[TO].push_back({i, Ptr});
      used[{i, TO}] = used[{TO, i}] = 1;
      ++Ptr;
    }
  }
  for (long long i = 1; i <= n; ++i) g[i] = gr[i];
  if ((long long)oks.size() == 2) {
    cout << "YES\n";
    cout << oks[0] << " " << oks[1] << " " << cost[g[oks[0]][0].second] << endl;
    return 0;
  }
  long long root = 0;
  for (long long i : oks) {
    if ((long long)g[i].size() > 1) root = i;
  }
  dfs(root);
  solve(root);
  cout << "YES\n";
  cout << (long long)ans.size() << endl;
  for (auto p : ans) {
    cout << p.u << " " << p.v << " " << p.c << '\n';
  }
  return 0;
}