#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
void eq(pair<A, B> &a, pair<A, B> b) {
  a.first = b.first;
  a.second = b.second;
}
const int N = 2e5 + 9;
vector<int> g[N];
int d[2][N];
int n;
pair<int, int> down[N];
pair<int, pair<int, int> > ans;
char marked[N];
pair<int, int> dfs1(int v, int par = -1) {
  pair<int, int> ret;
  eq(ret, make_pair(0, v));
  vector<pair<int, int> > vec;
  for (int to : g[v]) {
    if (to == par) continue;
    pair<int, int> cur;
    eq(cur, dfs1(to, v));
    vec.push_back(cur);
    ++cur.first;
    eq(ret, max(ret, cur));
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  if (vec.size() >= 2) {
    pair<int, pair<int, int> > cur_ans;
    cur_ans.first = vec[0].first + vec[1].first;
    eq(cur_ans.second, make_pair(vec[0].second, vec[1].second));
    eq(ans, max(ans, cur_ans));
  }
  return down[v] = ret;
}
void dfs2(int v, pair<int, int> up, int par = -1) {
  pair<int, pair<int, int> > cur_ans;
  cur_ans.first = up.first + 1 + down[v].first;
  eq(cur_ans.second, make_pair(up.second, down[v].second));
  eq(ans, max(ans, cur_ans));
  set<pair<int, int> > st;
  st.insert(up);
  for (int to : g[v]) {
    if (to == par) continue;
    st.insert(down[to]);
  }
  for (int to : g[v]) {
    if (to == par) continue;
    st.erase(down[to]);
    pair<int, int> mx;
    eq(mx, (*st.rbegin()));
    ++mx.first;
    dfs2(to, mx, v);
    st.insert(down[to]);
  }
}
void dist_dfs(int v, int z, int dist, int par) {
  d[z][v] = dist;
  for (int to : g[v]) {
    if (to == par) continue;
    dist_dfs(to, z, dist + 1, v);
  }
}
bool mark_dfs(int v, int dest, int par) {
  if (v == dest) {
    return marked[v] = true;
  }
  for (int to : g[v]) {
    if (to == par) continue;
    bool cur = mark_dfs(to, dest, v);
    if (cur) {
      return marked[v] = true;
    }
  }
  return false;
}
vector<pair<int, int> > res;
long long sum;
void ans_dfs(int v, int par) {
  for (int to : g[v]) {
    if (to == par) continue;
    ans_dfs(to, v);
  }
  if (!marked[v]) {
    int z = ans.second.first;
    if (d[1][v] > d[0][v]) z = ans.second.second;
    res.push_back(make_pair(z, v));
    sum += max(d[1][v], d[0][v]);
  }
}
void ans_dfs2(int v, int par) {
  if (!marked[v]) return;
  for (int to : g[v]) {
    if (to == par) continue;
    ans_dfs2(to, v);
  }
  if (v == ans.second.first) return;
  res.push_back(make_pair(ans.second.first, v));
  sum += d[0][v];
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  eq(ans, make_pair(-1, make_pair(-1, -1)));
  dfs1(1);
  dfs2(1, make_pair(-1, 1));
  dist_dfs(ans.second.first, 0, 0, -1);
  dist_dfs(ans.second.second, 1, 0, -1);
  mark_dfs(ans.second.first, ans.second.second, -1);
  ans_dfs(ans.second.first, -1);
  ans_dfs2(ans.second.first, -1);
  cout << sum << "\n";
  for (auto x : res)
    cout << x.first << " " << x.second << " " << x.second << "\n";
}

### Prompt

Your task is to create 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;
template <class A, class B>
void eq(pair<A, B> &a, pair<A, B> b) {
  a.first = b.first;
  a.second = b.second;
}
const int N = 2e5 + 9;
vector<int> g[N];
int d[2][N];
int n;
pair<int, int> down[N];
pair<int, pair<int, int> > ans;
char marked[N];
pair<int, int> dfs1(int v, int par = -1) {
  pair<int, int> ret;
  eq(ret, make_pair(0, v));
  vector<pair<int, int> > vec;
  for (int to : g[v]) {
    if (to == par) continue;
    pair<int, int> cur;
    eq(cur, dfs1(to, v));
    vec.push_back(cur);
    ++cur.first;
    eq(ret, max(ret, cur));
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  if (vec.size() >= 2) {
    pair<int, pair<int, int> > cur_ans;
    cur_ans.first = vec[0].first + vec[1].first;
    eq(cur_ans.second, make_pair(vec[0].second, vec[1].second));
    eq(ans, max(ans, cur_ans));
  }
  return down[v] = ret;
}
void dfs2(int v, pair<int, int> up, int par = -1) {
  pair<int, pair<int, int> > cur_ans;
  cur_ans.first = up.first + 1 + down[v].first;
  eq(cur_ans.second, make_pair(up.second, down[v].second));
  eq(ans, max(ans, cur_ans));
  set<pair<int, int> > st;
  st.insert(up);
  for (int to : g[v]) {
    if (to == par) continue;
    st.insert(down[to]);
  }
  for (int to : g[v]) {
    if (to == par) continue;
    st.erase(down[to]);
    pair<int, int> mx;
    eq(mx, (*st.rbegin()));
    ++mx.first;
    dfs2(to, mx, v);
    st.insert(down[to]);
  }
}
void dist_dfs(int v, int z, int dist, int par) {
  d[z][v] = dist;
  for (int to : g[v]) {
    if (to == par) continue;
    dist_dfs(to, z, dist + 1, v);
  }
}
bool mark_dfs(int v, int dest, int par) {
  if (v == dest) {
    return marked[v] = true;
  }
  for (int to : g[v]) {
    if (to == par) continue;
    bool cur = mark_dfs(to, dest, v);
    if (cur) {
      return marked[v] = true;
    }
  }
  return false;
}
vector<pair<int, int> > res;
long long sum;
void ans_dfs(int v, int par) {
  for (int to : g[v]) {
    if (to == par) continue;
    ans_dfs(to, v);
  }
  if (!marked[v]) {
    int z = ans.second.first;
    if (d[1][v] > d[0][v]) z = ans.second.second;
    res.push_back(make_pair(z, v));
    sum += max(d[1][v], d[0][v]);
  }
}
void ans_dfs2(int v, int par) {
  if (!marked[v]) return;
  for (int to : g[v]) {
    if (to == par) continue;
    ans_dfs2(to, v);
  }
  if (v == ans.second.first) return;
  res.push_back(make_pair(ans.second.first, v));
  sum += d[0][v];
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  eq(ans, make_pair(-1, make_pair(-1, -1)));
  dfs1(1);
  dfs2(1, make_pair(-1, 1));
  dist_dfs(ans.second.first, 0, 0, -1);
  dist_dfs(ans.second.second, 1, 0, -1);
  mark_dfs(ans.second.first, ans.second.second, -1);
  ans_dfs(ans.second.first, -1);
  ans_dfs2(ans.second.first, -1);
  cout << sum << "\n";
  for (auto x : res)
    cout << x.first << " " << x.second << " " << x.second << "\n";
}

```