#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vpii = vector<pii>;
template <class T>
inline bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool ckmax(T& a, const T& b) {
  return b > a ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const ll infLL = 1e18;
const ld eps = 1e-6;
int n;
vi adj[mxN];
int du[mxN], dv[mxN];
int U, V;
bool path[mxN];
vector<array<int, 3>> e;
vector<array<int, 3>> e2;
ll ans = 0;
void dfs(int u, int p, int* d) {
  for (auto& v : adj[u])
    if (p != v) {
      d[v] = d[u] + 1;
      dfs(v, u, d);
    }
}
void dfs2(int u, int p) {
  if (V == u) return;
  for (auto& v : adj[u])
    if (p != v) {
      dfs2(v, u);
      path[u] |= path[v];
    }
  if (path[u]) e2.push_back({u, V, u});
}
void dfs3(int u, int p) {
  for (auto& v : adj[u])
    if (p != v && !path[v]) {
      dfs3(v, u);
    }
  if (!path[u]) {
    ans += max(du[u], dv[u]);
    if (du[u] > dv[u]) {
      e.push_back({u, U, u});
    } else {
      e.push_back({u, V, u});
    }
  }
}
int32_t main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int i = (0); i < (n - 1); i++) {
    int e1, e2;
    cin >> e1 >> e2;
    e1--;
    e2--;
    adj[e1].push_back(e2);
    adj[e2].push_back(e1);
  }
  dfs(0, -1, du);
  U = max_element(du, du + n) - du;
  memset(du, 0, sizeof du);
  dfs(U, -1, du);
  V = max_element(du, du + n) - du;
  dfs(V, -1, dv);
  path[V] = 1;
  dfs2(U, -1);
  for (int i = (0); i < (n); i++) {
    if (path[i]) dfs3(i, -1);
  }
  ans += 1LL * (ll)du[V] * (ll)(du[V] + 1) / 2LL;
  reverse(e2.begin(), e2.end());
  e.insert(e.end(), e2.begin(), e2.end());
  cout << ans << nl;
  for (int i = (0); i < ((int)e.size()); i++) {
    cout << e[i][0] + 1 << ' ' << e[i][1] + 1 << ' ' << e[i][2] + 1 << nl;
  }
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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 ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vpii = vector<pii>;
template <class T>
inline bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool ckmax(T& a, const T& b) {
  return b > a ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const ll infLL = 1e18;
const ld eps = 1e-6;
int n;
vi adj[mxN];
int du[mxN], dv[mxN];
int U, V;
bool path[mxN];
vector<array<int, 3>> e;
vector<array<int, 3>> e2;
ll ans = 0;
void dfs(int u, int p, int* d) {
  for (auto& v : adj[u])
    if (p != v) {
      d[v] = d[u] + 1;
      dfs(v, u, d);
    }
}
void dfs2(int u, int p) {
  if (V == u) return;
  for (auto& v : adj[u])
    if (p != v) {
      dfs2(v, u);
      path[u] |= path[v];
    }
  if (path[u]) e2.push_back({u, V, u});
}
void dfs3(int u, int p) {
  for (auto& v : adj[u])
    if (p != v && !path[v]) {
      dfs3(v, u);
    }
  if (!path[u]) {
    ans += max(du[u], dv[u]);
    if (du[u] > dv[u]) {
      e.push_back({u, U, u});
    } else {
      e.push_back({u, V, u});
    }
  }
}
int32_t main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int i = (0); i < (n - 1); i++) {
    int e1, e2;
    cin >> e1 >> e2;
    e1--;
    e2--;
    adj[e1].push_back(e2);
    adj[e2].push_back(e1);
  }
  dfs(0, -1, du);
  U = max_element(du, du + n) - du;
  memset(du, 0, sizeof du);
  dfs(U, -1, du);
  V = max_element(du, du + n) - du;
  dfs(V, -1, dv);
  path[V] = 1;
  dfs2(U, -1);
  for (int i = (0); i < (n); i++) {
    if (path[i]) dfs3(i, -1);
  }
  ans += 1LL * (ll)du[V] * (ll)(du[V] + 1) / 2LL;
  reverse(e2.begin(), e2.end());
  e.insert(e.end(), e2.begin(), e2.end());
  cout << ans << nl;
  for (int i = (0); i < ((int)e.size()); i++) {
    cout << e[i][0] + 1 << ' ' << e[i][1] + 1 << ' ' << e[i][2] + 1 << nl;
  }
}

```