#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
vector<int> path;
vector<int> G[N];
int dis[N];
int fa[N][22];
int inpath[N];
int L[N];
int tot = 0;
int Max_Len = 0;
int root_u, root_v;
void init() {
  Max_Len = 0;
  path.clear();
  tot = 0;
  memset(inpath, 0, sizeof(inpath));
  for (int i = 0; i < N; i++) G[i].clear();
}
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
void init_lca(int u, int f, int d) {
  dis[u] = d;
  fa[u][0] = f;
  if (dis[u] >= Max_Len) Max_Len = dis[u], root_u = u;
  for (int i = 1; i < 22; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (auto v : G[u]) {
    if (v == f) continue;
    init_lca(v, u, d + 1);
  }
}
int lca(int u, int v) {
  if (dis[u] < dis[v]) swap(u, v);
  for (int i = 21; i >= 0; i--)
    if (dis[fa[u][i]] >= dis[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = 21; i >= 0; i--)
    if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
void find_root(int u, int fa, int d) {
  L[u] = ++tot;
  if (d >= Max_Len) root_v = u, Max_Len = d;
  for (auto v : G[u]) {
    if (v == fa) continue;
    find_root(v, u, d + 1);
  }
}
bool find_path(int u, int fa) {
  if (u == root_v) {
    inpath[u] = 1;
    path.push_back(u);
    return true;
  }
  for (auto v : G[u]) {
    if (v == fa) continue;
    if (find_path(v, u)) {
      inpath[u] = 1;
      path.push_back(u);
      return true;
    }
  }
  return false;
}
vector<pair<int, int> > ans;
int main() {
  ios ::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    init();
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v;
      add_edge(u, v);
    }
    init_lca(1, 1, 0);
    find_root(root_u, root_u, 0);
    find_path(root_u, root_u);
    ans.clear();
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      if (!inpath[i]) {
        long long ans1 = dis[root_u] + dis[i] - 2 * dis[lca(root_u, i)];
        long long ans2 = dis[root_v] + dis[i] - 2 * dis[lca(root_v, i)];
        sum += max(ans1, ans2);
        if (ans1 > ans2)
          ans.push_back(make_pair(root_u, i));
        else
          ans.push_back(make_pair(root_v, i));
      }
    }
    sort(ans.begin(), ans.end(), [](pair<int, int> a, pair<int, int> b) {
      return L[a.second] > L[b.second];
    });
    for (int i = 0; i < path.size() - 1; i++) {
      sum += dis[path[i]] + dis[root_u] - 2 * dis[lca(path[i], root_u)];
      ans.push_back(make_pair(root_u, path[i]));
    }
    cout << sum << endl;
    for (auto temp : ans)
      cout << temp.first << " " << temp.second << " " << temp.second << endl;
  }
}

### 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;
const int N = 3e5 + 7;
vector<int> path;
vector<int> G[N];
int dis[N];
int fa[N][22];
int inpath[N];
int L[N];
int tot = 0;
int Max_Len = 0;
int root_u, root_v;
void init() {
  Max_Len = 0;
  path.clear();
  tot = 0;
  memset(inpath, 0, sizeof(inpath));
  for (int i = 0; i < N; i++) G[i].clear();
}
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
void init_lca(int u, int f, int d) {
  dis[u] = d;
  fa[u][0] = f;
  if (dis[u] >= Max_Len) Max_Len = dis[u], root_u = u;
  for (int i = 1; i < 22; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (auto v : G[u]) {
    if (v == f) continue;
    init_lca(v, u, d + 1);
  }
}
int lca(int u, int v) {
  if (dis[u] < dis[v]) swap(u, v);
  for (int i = 21; i >= 0; i--)
    if (dis[fa[u][i]] >= dis[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = 21; i >= 0; i--)
    if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
void find_root(int u, int fa, int d) {
  L[u] = ++tot;
  if (d >= Max_Len) root_v = u, Max_Len = d;
  for (auto v : G[u]) {
    if (v == fa) continue;
    find_root(v, u, d + 1);
  }
}
bool find_path(int u, int fa) {
  if (u == root_v) {
    inpath[u] = 1;
    path.push_back(u);
    return true;
  }
  for (auto v : G[u]) {
    if (v == fa) continue;
    if (find_path(v, u)) {
      inpath[u] = 1;
      path.push_back(u);
      return true;
    }
  }
  return false;
}
vector<pair<int, int> > ans;
int main() {
  ios ::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    init();
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v;
      add_edge(u, v);
    }
    init_lca(1, 1, 0);
    find_root(root_u, root_u, 0);
    find_path(root_u, root_u);
    ans.clear();
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      if (!inpath[i]) {
        long long ans1 = dis[root_u] + dis[i] - 2 * dis[lca(root_u, i)];
        long long ans2 = dis[root_v] + dis[i] - 2 * dis[lca(root_v, i)];
        sum += max(ans1, ans2);
        if (ans1 > ans2)
          ans.push_back(make_pair(root_u, i));
        else
          ans.push_back(make_pair(root_v, i));
      }
    }
    sort(ans.begin(), ans.end(), [](pair<int, int> a, pair<int, int> b) {
      return L[a.second] > L[b.second];
    });
    for (int i = 0; i < path.size() - 1; i++) {
      sum += dis[path[i]] + dis[root_u] - 2 * dis[lca(path[i], root_u)];
      ans.push_back(make_pair(root_u, path[i]));
    }
    cout << sum << endl;
    for (auto temp : ans)
      cout << temp.first << " " << temp.second << " " << temp.second << endl;
  }
}

```