#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int LOG_N = 60;
int n;
vector<int> G[MAXN];
int depth[MAXN];
int parent[LOG_N][MAXN];
void dfs(int pre, int u, int d) {
  parent[0][u] = pre;
  depth[u] = d;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v != pre) dfs(u, v, d + 1);
  }
}
void init() {
  int root = 1;
  dfs(-1, root, 0);
  for (int k = 1; k < LOG_N; k++) {
    for (int i = 1; i <= n; i++) {
      if (parent[k - 1][i] < 0)
        parent[k][i] = -1;
      else
        parent[k][i] = parent[k - 1][parent[k - 1][i]];
    }
  }
}
int lca(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int i = 0; i < LOG_N; i++) {
    if ((depth[v] - depth[u]) >> i & 1) {
      v = parent[i][v];
    }
  }
  if (v == u) return u;
  for (int i = LOG_N - 1; i >= 0; i--) {
    if (parent[i][u] != parent[i][v]) {
      u = parent[i][u];
      v = parent[i][v];
    }
  }
  return parent[0][u];
}
int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
int x1, x2, pos, mxl, vs[MAXN];
void dfs1(int fa, int u, int l) {
  if (fa && l > mxl) {
    mxl = l;
    pos = u;
  }
  for (int v : G[u]) {
    if (v != fa) {
      dfs1(u, v, l + 1);
    }
  }
}
int cc, a[MAXN], b[MAXN], c[MAXN], chain[MAXN];
long long ans;
void dfs2(int fa, int u, int xx, int dd) {
  for (int v : G[u]) {
    if (!vs[v] && v != fa) {
      dfs2(u, v, xx, dd + 1);
      a[cc] = xx;
      b[cc] = v;
      c[cc++] = v;
      ans += dd + 1;
    }
  }
}
void solve(int x) {
  int d1 = dist(x, x1), d2 = dist(x, x2), dd, xx;
  if (d1 > d2) {
    dd = d1;
    xx = x1;
  } else {
    dd = d2;
    xx = x2;
  }
  dfs2(0, x, xx, dd);
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  init();
  dfs1(0, 1, 0);
  mxl = 0;
  x1 = pos;
  dfs1(0, x1, 0);
  x2 = pos;
  int comm = lca(x1, x2);
  int x = x1, xc = 0;
  vs[comm] = 1;
  while (x != comm) {
    vs[x] = 1;
    chain[xc++] = x;
    x = parent[0][x];
  }
  chain[xc++] = comm;
  xc = mxl;
  x = x2;
  while (x != comm) {
    vs[x] = 1;
    chain[xc--] = x;
    x = parent[0][x];
  }
  for (int i = 0; i <= mxl; i++) {
    solve(chain[i]);
  }
  for (int i = 0; i < mxl; i++) {
    a[cc] = chain[i];
    b[cc] = chain[mxl];
    c[cc++] = chain[i];
    ans += mxl - i;
  }
  cout << ans << endl;
  for (int i = 0; i < n - 1; i++) {
    printf("%d %d %d\n", a[i], b[i], c[i]);
  }
  return 0;
}

### Prompt

In Cpp, your task is to solve 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 MAXN = 2e5 + 5;
const int LOG_N = 60;
int n;
vector<int> G[MAXN];
int depth[MAXN];
int parent[LOG_N][MAXN];
void dfs(int pre, int u, int d) {
  parent[0][u] = pre;
  depth[u] = d;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v != pre) dfs(u, v, d + 1);
  }
}
void init() {
  int root = 1;
  dfs(-1, root, 0);
  for (int k = 1; k < LOG_N; k++) {
    for (int i = 1; i <= n; i++) {
      if (parent[k - 1][i] < 0)
        parent[k][i] = -1;
      else
        parent[k][i] = parent[k - 1][parent[k - 1][i]];
    }
  }
}
int lca(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int i = 0; i < LOG_N; i++) {
    if ((depth[v] - depth[u]) >> i & 1) {
      v = parent[i][v];
    }
  }
  if (v == u) return u;
  for (int i = LOG_N - 1; i >= 0; i--) {
    if (parent[i][u] != parent[i][v]) {
      u = parent[i][u];
      v = parent[i][v];
    }
  }
  return parent[0][u];
}
int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
int x1, x2, pos, mxl, vs[MAXN];
void dfs1(int fa, int u, int l) {
  if (fa && l > mxl) {
    mxl = l;
    pos = u;
  }
  for (int v : G[u]) {
    if (v != fa) {
      dfs1(u, v, l + 1);
    }
  }
}
int cc, a[MAXN], b[MAXN], c[MAXN], chain[MAXN];
long long ans;
void dfs2(int fa, int u, int xx, int dd) {
  for (int v : G[u]) {
    if (!vs[v] && v != fa) {
      dfs2(u, v, xx, dd + 1);
      a[cc] = xx;
      b[cc] = v;
      c[cc++] = v;
      ans += dd + 1;
    }
  }
}
void solve(int x) {
  int d1 = dist(x, x1), d2 = dist(x, x2), dd, xx;
  if (d1 > d2) {
    dd = d1;
    xx = x1;
  } else {
    dd = d2;
    xx = x2;
  }
  dfs2(0, x, xx, dd);
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  init();
  dfs1(0, 1, 0);
  mxl = 0;
  x1 = pos;
  dfs1(0, x1, 0);
  x2 = pos;
  int comm = lca(x1, x2);
  int x = x1, xc = 0;
  vs[comm] = 1;
  while (x != comm) {
    vs[x] = 1;
    chain[xc++] = x;
    x = parent[0][x];
  }
  chain[xc++] = comm;
  xc = mxl;
  x = x2;
  while (x != comm) {
    vs[x] = 1;
    chain[xc--] = x;
    x = parent[0][x];
  }
  for (int i = 0; i <= mxl; i++) {
    solve(chain[i]);
  }
  for (int i = 0; i < mxl; i++) {
    a[cc] = chain[i];
    b[cc] = chain[mxl];
    c[cc++] = chain[i];
    ans += mxl - i;
  }
  cout << ans << endl;
  for (int i = 0; i < n - 1; i++) {
    printf("%d %d %d\n", a[i], b[i], c[i]);
  }
  return 0;
}

```