#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using pill = pair<int, ll>;
using vvi = vector<vi>;
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
ll fast_exp(ll base, ll n, ll m) {
  if (n == 0) return 1;
  ll t = fast_exp(base, n / 2, m);
  if (n % 2 == 0)
    return (t * t) % m;
  else
    return (((t * t) % m) * base) % m;
}
int const N = 2e5;
vector<set<int>> g(N + 1);
int dis[N + 1][2];
void dfs(int x, int p, int l, int& root, int& ml) {
  if (l > ml) ml = l, root = x;
  for (auto& i : g[x])
    if (i != p) dfs(i, x, l + 1, root, ml);
}
void dfs1(int x, int p, int l, int r) {
  dis[x][r] = l;
  for (auto& i : g[x])
    if (i != p) dfs1(i, x, l + 1, r);
}
using a3 = array<int, 3>;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].insert(v), g[v].insert(u);
  }
  int ml = 0, e1 = 0, e2 = 0;
  dfs(1, 0, 0, e1, ml);
  ml = 0;
  dfs(e1, 0, 0, e2, ml);
  dfs1(e1, 0, 0, 0);
  dfs1(e2, 0, 0, 1);
  queue<int> q;
  for (int i = 1; i <= n; ++i)
    if (i != e1 and i != e2 and g[i].size() == 1) q.push(i);
  ll ans = 0;
  vector<a3> v;
  while (not q.empty()) {
    int x = q.front();
    if (dis[x][0] > dis[x][1])
      ans += dis[x][0], v.push_back({x, e1, x});
    else
      ans += dis[x][1], v.push_back({x, e2, x});
    auto i = *g[x].begin();
    g[i].erase(x);
    if (g[i].size() == 1) q.push(i);
    q.pop();
  }
  q.push(e2);
  while (not q.empty()) {
    int x = q.front();
    ans += dis[x][0];
    v.push_back({x, e1, x});
    auto i = *g[x].begin();
    g[i].erase(x);
    if (g[i].size() == 1) q.push(i);
    q.pop();
  }
  cout << ans << "\n";
  for (auto& i : v) cout << i[0] << " " << i[1] << " " << i[2] << "\n";
  return 0;
}

### Prompt

Generate a cpp solution to the following problem:

You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps: 

  1. choose two leaves; 
  2. add the length of the simple path between them to the answer; 
  3. remove one of the chosen leaves from the tree. 



Initial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex. 

Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!

Input

The first line contains one integer number n (2 ≤ n ≤ 2·105) — the number of vertices in the tree. 

Next n - 1 lines describe the edges of the tree in form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that given graph is a tree.

Output

In the first line print one integer number — maximal possible answer. 

In the next n - 1 lines print the operations in order of their applying in format ai, bi, ci, where ai, bi — pair of the leaves that are chosen in the current operation (1 ≤ ai, bi ≤ n), ci (1 ≤ ci ≤ n, ci = ai or ci = bi) — choosen leaf that is removed from the tree in the current operation. 

See the examples for better understanding.

Examples

Input

3
1 2
1 3


Output

3
2 3 3
2 1 1


Input

5
1 2
1 3
2 4
2 5


Output

9
3 5 5
4 3 3
4 1 1
4 2 2

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using pill = pair<int, ll>;
using vvi = vector<vi>;
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
ll fast_exp(ll base, ll n, ll m) {
  if (n == 0) return 1;
  ll t = fast_exp(base, n / 2, m);
  if (n % 2 == 0)
    return (t * t) % m;
  else
    return (((t * t) % m) * base) % m;
}
int const N = 2e5;
vector<set<int>> g(N + 1);
int dis[N + 1][2];
void dfs(int x, int p, int l, int& root, int& ml) {
  if (l > ml) ml = l, root = x;
  for (auto& i : g[x])
    if (i != p) dfs(i, x, l + 1, root, ml);
}
void dfs1(int x, int p, int l, int r) {
  dis[x][r] = l;
  for (auto& i : g[x])
    if (i != p) dfs1(i, x, l + 1, r);
}
using a3 = array<int, 3>;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].insert(v), g[v].insert(u);
  }
  int ml = 0, e1 = 0, e2 = 0;
  dfs(1, 0, 0, e1, ml);
  ml = 0;
  dfs(e1, 0, 0, e2, ml);
  dfs1(e1, 0, 0, 0);
  dfs1(e2, 0, 0, 1);
  queue<int> q;
  for (int i = 1; i <= n; ++i)
    if (i != e1 and i != e2 and g[i].size() == 1) q.push(i);
  ll ans = 0;
  vector<a3> v;
  while (not q.empty()) {
    int x = q.front();
    if (dis[x][0] > dis[x][1])
      ans += dis[x][0], v.push_back({x, e1, x});
    else
      ans += dis[x][1], v.push_back({x, e2, x});
    auto i = *g[x].begin();
    g[i].erase(x);
    if (g[i].size() == 1) q.push(i);
    q.pop();
  }
  q.push(e2);
  while (not q.empty()) {
    int x = q.front();
    ans += dis[x][0];
    v.push_back({x, e1, x});
    auto i = *g[x].begin();
    g[i].erase(x);
    if (g[i].size() == 1) q.push(i);
    q.pop();
  }
  cout << ans << "\n";
  for (auto& i : v) cout << i[0] << " " << i[1] << " " << i[2] << "\n";
  return 0;
}

```