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>
#include <bits/stdc++.h>
inline int min(const int &first, const int &second) {
  return (((second - first) >> (32 - 1)) & (first ^ second)) ^ first;
}
inline int max(const int &first, const int &second) {
  return (((second - first) >> (32 - 1)) & (first ^ second)) ^ second;
}
inline long long min(const long long &first, const long long &second) {
  return (((second - first) >> (64 - 1)) & (first ^ second)) ^ first;
}
inline long long max(const long long &first, const long long &second) {
  return (((second - first) >> (64 - 1)) & (first ^ second)) ^ second;
}
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &first) { scanf("%d", &first); }
void fast_scan(long long &first) { scanf("%lld", &first); }
void fast_scan(unsigned long long &first) { scanf("%llu", &first); }
void fast_scan(double &first) { scanf("%lf", &first); }
void fast_scan(long double &first) { scanf("%Lf", &first); }
void fast_scan(char &first) {
  scanf("%c", &first);
  if (first == '\n') {
    fast_scan(first);
  }
}
void fast_scan(string &first) {
  scanf("%s", string_in_buffer);
  first = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
  fast_scan(p.first);
  fast_scan(p.second);
}
template <class T>
void fast_scan(vector<T> &v) {
  for (auto &first : v) fast_scan(first);
}
void fast_print(const int &first) { printf("%d", first); }
void fast_print(const unsigned int &first) { printf("%u", first); }
void fast_print(const long long &first) { printf("%lld", first); }
void fast_print(const unsigned long long &first) { printf("%llu", first); }
void fast_print(const double &first) { printf("%.15lf", first); }
void fast_print(const long double &first) { printf("%.15Lf", first); }
void fast_print(const char &first) { printf("%c", first); };
void fast_print(const string &first) { printf("%s", first.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
  fast_print(p.first);
  fast_print(' ');
  fast_print(p.second);
}
template <class T>
void fast_print(const vector<T> &v) {
  if (v.empty()) return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print(' ');
    fast_print(v[i]);
  }
}
template <class T>
void fast_print(const vector<vector<T>> &v) {
  if (v.empty()) return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print('\n');
    fast_print(v[i]);
  }
}
template <class T>
void fast_print(const T &v) {
  for (const auto &first : v) {
    fast_print(first);
    fast_print(' ');
  }
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
  fast_print(print_start);
  print_start = "\n";
  first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
  if (!first_print) {
    fast_print("\n");
  } else {
    first_print = false;
  }
  fast_print(names.front());
  fast_print(" = ");
  fast_print(elem);
  names.pop_front();
  _print(names, args...);
}
}  // namespace smart_io
template <class T>
ostream &operator,(ostream &os, const T &object) {
  if (!smart_io::first_print) {
    fast_print(smart_io::sep);
  } else {
    smart_io::first_print = false;
  }
  fast_print(object);
  return os;
}
template <class T>
istream &operator,(istream &is, T &object) {
  fast_scan(object);
  return is;
}
namespace escape__from__random__aetuhoetnuhshe {
using namespace std::chrono;
mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch())
                .count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
};  // namespace escape__from__random__aetuhoetnuhshe
namespace typedefs {}
namespace numbers_operation {
template <class T>
T floor_mod(T a, T b) {
  if (a >= 0 && b >= 0) return a % b;
  if (a <= 0 && b <= 0) return a % b;
  return abs(b) - (abs(a) % abs(b));
}
}  // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
using namespace escape__from__random__aetuhoetnuhshe;
long long n;
vector<vector<pair<long long, long long>>> g;
vector<long long> leaf;
vector<long long> depth;
vector<long long> value;
vector<long long> pref;
void dfs(long long v, long long _p) {
  leaf[v] = v;
  pref[v] = _p;
  for (pair<long long, long long> p : g[v]) {
    if (p.first == _p) continue;
    depth[p.first] = depth[v] + 1;
    value[p.first] = p.second;
    dfs(p.first, v);
    leaf[v] = leaf[p.first];
  }
}
vector<pair<pair<long long, long long>, long long>> acts;
void inc(long long a, long long b, long long d) {
  acts.emplace_back(make_pair(make_pair(a + 1, b + 1), -d));
  while (a != b) {
    if (depth[a] < depth[b]) swap(a, b);
    value[a] += d;
    a = pref[a];
  }
}
void go(long long v, long long _p, long long up) {
  vector<long long> childs;
  for (pair<long long, long long> p : g[v]) {
    if (p.first == _p) continue;
    childs.push_back(p.first);
  }
  if (((long long)childs.size()) == 0) return;
  if (((long long)childs.size()) == 1) {
    if (value[childs[0]] != 0) {
      smart_io::precall_print();
      cout, "NO";
      exit(0);
    }
  } else if (((long long)childs.size()) == 2) {
    inc(leaf[childs[0]], leaf[childs[1]],
        (-value[childs[0]] - value[childs[1]]) / 2);
    inc(up, leaf[childs[0]], -value[childs[0]]);
    inc(up, leaf[childs[1]], -value[childs[1]]);
  } else {
    for (long long i = ((long long)childs.size()) - 1; i >= 3; i--) {
      inc(leaf[childs[0]], leaf[childs[i]], -value[childs[i]]);
    }
    long long a = value[childs[0]];
    long long b = value[childs[1]];
    long long c = value[childs[2]];
    swap(b, c);
    long long first = a / 2 - b / 2 + c / 2;
    long long second = a / 2 + b / 2 - c / 2;
    long long z = -a / 2 + b / 2 + c / 2;
    inc(leaf[childs[0]], leaf[childs[1]], -first);
    inc(leaf[childs[0]], leaf[childs[2]], -second);
    inc(leaf[childs[1]], leaf[childs[2]], -z);
  }
  for (long long sub : childs) {
    assert(value[sub] == 0);
  }
  for (long long sub : childs) {
    long long l = -1;
    for (long long second : childs) {
      if (sub != second) {
        l = leaf[second];
        break;
      }
    }
    assert(l != -1);
    go(sub, v, l);
  }
}
signed main(signed argc, char *argv[]) {
  cin, n;
  g.resize(n);
  value.resize(n);
  set<long long> all;
  for (long long i = 0; i < (n - 1); i++) {
    long long a, b, w;
    cin, a, b, w;
    a--;
    b--;
    all.insert(w);
    g[a].emplace_back(b, w);
    g[b].emplace_back(a, w);
  }
  if (n == 2) {
    smart_io::precall_print();
    cout, "YES";
    smart_io::precall_print();
    cout, 1;
    smart_io::precall_print();
    cout, 1, 2, g[0][0].second;
    return 0;
  }
  long long root = 0;
  for (long long i = 0; i < (n); i++) {
    if (((long long)g[i].size()) > ((long long)g[root].size())) {
      root = i;
    }
  }
  if (((long long)g[root].size()) == 2) {
    if (((long long)all.size()) == 1) {
      smart_io::precall_print();
      cout, "YES";
      smart_io::precall_print();
      cout, 1;
      vector<long long> leafs;
      for (long long i = 0; i < (n); i++) {
        if (((long long)g[i].size()) == 1) {
          leafs.push_back(i);
        }
      }
      smart_io::precall_print();
      cout, leafs[0] + 1, leafs[1] + 1, *all.begin();
    } else {
      smart_io::precall_print();
      cout, "NO";
    }
    return 0;
  }
  leaf.resize(n);
  depth.resize(n);
  pref.resize(n);
  dfs(root, -1);
  go(root, -1, -1);
  smart_io::precall_print();
  cout, "YES";
  smart_io::precall_print();
  cout, ((long long)acts.size());
  for (auto t : acts) {
    smart_io::precall_print();
    cout, t;
  }
}
