#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
vector<long long> e[N];
queue<long long> q;
long long n, fa[N], d[N], vis[N];
long long u[N], v[N], deg[N];
long long P, Q, dis1[N], dis2[N], ans;
long long MAX(long long x, long long y) { return x > y ? x : y; }
void dfs(long long x) {
  d[x] = d[fa[x]] + 1;
  for (long long i = 0; i < e[x].size(); i++)
    if (e[x][i] != fa[x]) fa[e[x][i]] = x, dfs(e[x][i]);
}
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i < n; i++) {
    scanf("%lld%lld", &u[i], &v[i]);
    e[u[i]].push_back(v[i]);
    e[v[i]].push_back(u[i]);
  }
  dfs(1);
  for (long long i = 1; i <= n; i++)
    if (d[i] > d[P]) P = i;
  memset(fa, 0, sizeof(fa));
  dfs(P);
  for (long long i = 1; i <= n; i++)
    if (d[i] > d[Q]) Q = i;
  for (long long i = 1; i <= n; i++) dis1[i] = d[i] - 1;
  memset(fa, 0, sizeof(fa));
  dfs(Q);
  for (long long i = 1; i <= n; i++) dis2[i] = d[i] - 1;
  for (long long i = 1; i < n; i++) {
    if (fa[u[i]] == v[i])
      deg[v[i]]++;
    else
      deg[u[i]]++;
  }
  for (long long i = 1; i <= n; i++)
    if (!deg[i]) q.push(i), vis[i] = 1;
  while (q.size()) {
    long long x = q.front();
    q.pop();
    if (x == P || x == Q) continue;
    ans += MAX(dis1[x], dis2[x]);
    deg[fa[x]]--;
    if (!deg[fa[x]]) q.push(fa[x]), vis[fa[x]] = 1;
  }
  while (q.size()) q.pop();
  for (long long i = 1; i <= n; i++)
    if (i == P || i == Q || !vis[i]) q.push(i);
  while (q.size()) q.pop(), ans += q.size();
  printf("%lld\n", ans);
  memset(deg, 0, sizeof(deg));
  memset(vis, 0, sizeof(vis));
  for (long long i = 1; i < n; i++) {
    if (fa[u[i]] == v[i])
      deg[v[i]]++;
    else
      deg[u[i]]++;
  }
  for (long long i = 1; i <= n; i++)
    if (!deg[i]) q.push(i), vis[i] = 1;
  while (q.size()) {
    long long x = q.front();
    q.pop();
    if (x == P || x == Q) continue;
    if (dis1[x] > dis2[x])
      printf("%lld %lld %lld\n", P, x, x);
    else
      printf("%lld %lld %lld\n", Q, x, x);
    deg[fa[x]]--;
    if (!deg[fa[x]]) q.push(fa[x]), vis[fa[x]] = 1;
  }
  while (q.size()) q.pop();
  long long t = 0;
  for (long long i = 1; i <= n; i++)
    if ((i == P || i == Q) && !deg[i]) {
      t = i;
      break;
    }
  if (t == P)
    t = Q;
  else
    t = P;
  for (long long i = 1; i <= n; i++)
    if ((i == P || i == Q || !vis[i]) && !deg[i]) q.push(i);
  while (q.size()) {
    long long x = q.front();
    q.pop();
    if (t == x) break;
    printf("%lld %lld %lld\n", t, x, x);
    deg[fa[x]]--;
    if (!deg[fa[x]]) q.push(fa[x]), vis[fa[x]] = 1;
  }
  return 0;
}

### 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 long long N = 2e5 + 10;
vector<long long> e[N];
queue<long long> q;
long long n, fa[N], d[N], vis[N];
long long u[N], v[N], deg[N];
long long P, Q, dis1[N], dis2[N], ans;
long long MAX(long long x, long long y) { return x > y ? x : y; }
void dfs(long long x) {
  d[x] = d[fa[x]] + 1;
  for (long long i = 0; i < e[x].size(); i++)
    if (e[x][i] != fa[x]) fa[e[x][i]] = x, dfs(e[x][i]);
}
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i < n; i++) {
    scanf("%lld%lld", &u[i], &v[i]);
    e[u[i]].push_back(v[i]);
    e[v[i]].push_back(u[i]);
  }
  dfs(1);
  for (long long i = 1; i <= n; i++)
    if (d[i] > d[P]) P = i;
  memset(fa, 0, sizeof(fa));
  dfs(P);
  for (long long i = 1; i <= n; i++)
    if (d[i] > d[Q]) Q = i;
  for (long long i = 1; i <= n; i++) dis1[i] = d[i] - 1;
  memset(fa, 0, sizeof(fa));
  dfs(Q);
  for (long long i = 1; i <= n; i++) dis2[i] = d[i] - 1;
  for (long long i = 1; i < n; i++) {
    if (fa[u[i]] == v[i])
      deg[v[i]]++;
    else
      deg[u[i]]++;
  }
  for (long long i = 1; i <= n; i++)
    if (!deg[i]) q.push(i), vis[i] = 1;
  while (q.size()) {
    long long x = q.front();
    q.pop();
    if (x == P || x == Q) continue;
    ans += MAX(dis1[x], dis2[x]);
    deg[fa[x]]--;
    if (!deg[fa[x]]) q.push(fa[x]), vis[fa[x]] = 1;
  }
  while (q.size()) q.pop();
  for (long long i = 1; i <= n; i++)
    if (i == P || i == Q || !vis[i]) q.push(i);
  while (q.size()) q.pop(), ans += q.size();
  printf("%lld\n", ans);
  memset(deg, 0, sizeof(deg));
  memset(vis, 0, sizeof(vis));
  for (long long i = 1; i < n; i++) {
    if (fa[u[i]] == v[i])
      deg[v[i]]++;
    else
      deg[u[i]]++;
  }
  for (long long i = 1; i <= n; i++)
    if (!deg[i]) q.push(i), vis[i] = 1;
  while (q.size()) {
    long long x = q.front();
    q.pop();
    if (x == P || x == Q) continue;
    if (dis1[x] > dis2[x])
      printf("%lld %lld %lld\n", P, x, x);
    else
      printf("%lld %lld %lld\n", Q, x, x);
    deg[fa[x]]--;
    if (!deg[fa[x]]) q.push(fa[x]), vis[fa[x]] = 1;
  }
  while (q.size()) q.pop();
  long long t = 0;
  for (long long i = 1; i <= n; i++)
    if ((i == P || i == Q) && !deg[i]) {
      t = i;
      break;
    }
  if (t == P)
    t = Q;
  else
    t = P;
  for (long long i = 1; i <= n; i++)
    if ((i == P || i == Q || !vis[i]) && !deg[i]) q.push(i);
  while (q.size()) {
    long long x = q.front();
    q.pop();
    if (t == x) break;
    printf("%lld %lld %lld\n", t, x, x);
    deg[fa[x]]--;
    if (!deg[fa[x]]) q.push(fa[x]), vis[fa[x]] = 1;
  }
  return 0;
}

```