#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T gi() {
  T f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;
int n;
int tot, head[N], ver[M], nxt[M];
int mx, id, dis[N], pre[N];
bool vis[N];
int fa[N], dep[N], son[N], sz[N], topp[N];
int seq[N], cnt;
vector<int> zj;
struct Ans {
  int a, b, c;
};
vector<Ans> Program;
int dep1[N];
inline void add(int u, int v) {
  ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
inline void bfs(int u) {
  queue<int> q;
  q.push(u);
  memset(dis, 0, sizeof dis);
  memset(vis, false, sizeof vis);
  memset(pre, 0, sizeof pre);
  vis[u] = true;
  mx = id = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = nxt[i]) {
      int v = ver[i];
      if (vis[v]) continue;
      vis[v] = true;
      dis[v] = dis[u] + 1;
      pre[v] = u;
      if (dis[v] > mx) mx = dis[v], id = v;
      q.push(v);
    }
  }
}
void dfs_dep(int u, int f) {
  dep1[u] = dep1[f] + 1;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (v == f) continue;
    dfs_dep(v, u);
  }
}
inline bool cmp(int x, int y) { return dep1[x] > dep1[y]; }
namespace LCA {
void dfs1(int u, int f) {
  sz[u] = 1, dep[u] = dep[f] + 1, fa[u] = f;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (v == f) continue;
    dfs1(v, u);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int f) {
  topp[u] = f;
  if (!son[u]) return;
  dfs2(son[u], f);
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (v == fa[u] || v == son[u]) continue;
    dfs2(v, v);
  }
}
int getLCA(int u, int v) {
  while (topp[u] != topp[v]) {
    if (dep[topp[u]] < dep[topp[v]]) swap(u, v);
    u = fa[topp[u]];
  }
  if (dep[u] < dep[v]) return u;
  return v;
}
}  // namespace LCA
int main() {
  n = gi<int>();
  for (int i = 1; i < n; i += 1) {
    int u = gi<int>(), v = gi<int>();
    add(u, v), add(v, u);
  }
  bfs(1);
  int lft = id;
  bfs(lft);
  int rght = id;
  LCA ::dfs1(1, 0);
  LCA ::dfs2(1, 1);
  memset(vis, false, sizeof vis);
  int now = rght;
  vis[now] = true;
  zj.push_back(now);
  while (now != lft) now = pre[now], vis[now] = true, zj.push_back(now);
  for (int i = 1; i <= n; i += 1)
    if (!vis[i]) seq[++cnt] = i;
  dfs_dep(lft, 0);
  sort(seq + 1, seq + 1 + cnt, cmp);
  long long ans = 0;
  for (int i = 1; i <= cnt; i += 1) {
    int u = seq[i];
    int lca1 = LCA ::getLCA(u, lft), lca2 = LCA ::getLCA(u, rght);
    int dis1 = dep[u] + dep[lft] - 2 * dep[lca1],
        dis2 = dep[u] + dep[rght] - 2 * dep[lca2];
    if (dis1 >= dis2)
      ans += dis1, Program.push_back((Ans){u, lft, u});
    else
      ans += dis2, Program.push_back((Ans){u, rght, u});
  }
  for (int i = 0; i < (int)zj.size() - 1; i += 1)
    ans += mx - i, Program.push_back((Ans){zj[i], lft, zj[i]});
  printf("%lld\n", ans);
  for (int i = 0; i < n - 1; i += 1)
    printf("%d %d %d\n", Program[i].a, Program[i].b, Program[i].c);
  return 0;
}

### Prompt

Please formulate 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 <typename T>
inline T gi() {
  T f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;
int n;
int tot, head[N], ver[M], nxt[M];
int mx, id, dis[N], pre[N];
bool vis[N];
int fa[N], dep[N], son[N], sz[N], topp[N];
int seq[N], cnt;
vector<int> zj;
struct Ans {
  int a, b, c;
};
vector<Ans> Program;
int dep1[N];
inline void add(int u, int v) {
  ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
inline void bfs(int u) {
  queue<int> q;
  q.push(u);
  memset(dis, 0, sizeof dis);
  memset(vis, false, sizeof vis);
  memset(pre, 0, sizeof pre);
  vis[u] = true;
  mx = id = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = nxt[i]) {
      int v = ver[i];
      if (vis[v]) continue;
      vis[v] = true;
      dis[v] = dis[u] + 1;
      pre[v] = u;
      if (dis[v] > mx) mx = dis[v], id = v;
      q.push(v);
    }
  }
}
void dfs_dep(int u, int f) {
  dep1[u] = dep1[f] + 1;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (v == f) continue;
    dfs_dep(v, u);
  }
}
inline bool cmp(int x, int y) { return dep1[x] > dep1[y]; }
namespace LCA {
void dfs1(int u, int f) {
  sz[u] = 1, dep[u] = dep[f] + 1, fa[u] = f;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (v == f) continue;
    dfs1(v, u);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int f) {
  topp[u] = f;
  if (!son[u]) return;
  dfs2(son[u], f);
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (v == fa[u] || v == son[u]) continue;
    dfs2(v, v);
  }
}
int getLCA(int u, int v) {
  while (topp[u] != topp[v]) {
    if (dep[topp[u]] < dep[topp[v]]) swap(u, v);
    u = fa[topp[u]];
  }
  if (dep[u] < dep[v]) return u;
  return v;
}
}  // namespace LCA
int main() {
  n = gi<int>();
  for (int i = 1; i < n; i += 1) {
    int u = gi<int>(), v = gi<int>();
    add(u, v), add(v, u);
  }
  bfs(1);
  int lft = id;
  bfs(lft);
  int rght = id;
  LCA ::dfs1(1, 0);
  LCA ::dfs2(1, 1);
  memset(vis, false, sizeof vis);
  int now = rght;
  vis[now] = true;
  zj.push_back(now);
  while (now != lft) now = pre[now], vis[now] = true, zj.push_back(now);
  for (int i = 1; i <= n; i += 1)
    if (!vis[i]) seq[++cnt] = i;
  dfs_dep(lft, 0);
  sort(seq + 1, seq + 1 + cnt, cmp);
  long long ans = 0;
  for (int i = 1; i <= cnt; i += 1) {
    int u = seq[i];
    int lca1 = LCA ::getLCA(u, lft), lca2 = LCA ::getLCA(u, rght);
    int dis1 = dep[u] + dep[lft] - 2 * dep[lca1],
        dis2 = dep[u] + dep[rght] - 2 * dep[lca2];
    if (dis1 >= dis2)
      ans += dis1, Program.push_back((Ans){u, lft, u});
    else
      ans += dis2, Program.push_back((Ans){u, rght, u});
  }
  for (int i = 0; i < (int)zj.size() - 1; i += 1)
    ans += mx - i, Program.push_back((Ans){zj[i], lft, zj[i]});
  printf("%lld\n", ans);
  for (int i = 0; i < n - 1; i += 1)
    printf("%d %d %d\n", Program[i].a, Program[i].b, Program[i].c);
  return 0;
}

```