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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, pointleft, pointright, maxdis, tt, flag;
int fa[(200000 + 5)], vis[(200000 + 5)], depth[(200000 + 5)], tag[(200000 + 5)],
    dis[(200000 + 5)], road[(200000 + 5)];
int disl[(200000 + 5)], disr[(200000 + 5)], son[(200000 + 5)];
int a[(200000 + 5)], b[(200000 + 5)], c[(200000 + 5)], rt;
long long ans;
vector<int> edge[(200000 + 5)];
queue<int> leaf;
void bfs(int u) {
  queue<int> q;
  q.push(u);
  vis[u] = 1;
  while (!q.empty()) {
    int p = q.front();
    vis[p] = 1;
    q.pop();
    for (int i = 0; i < edge[p].size(); i++) {
      int now = edge[p][i];
      if (!vis[now]) {
        dis[now] = dis[p] + 1;
        q.push(now);
      }
    }
  }
}
void dfs(int u) {
  if (u == pointright) {
    flag = 1;
    tag[u] = 1;
    road[++tt] = u;
    return;
  }
  for (int i = 0; i < edge[u].size(); i++) {
    int now = edge[u][i];
    if (!vis[now]) {
      vis[now] = 1;
      dfs(now);
    }
    if (flag == 1) {
      tag[u] = 1;
      road[++tt] = u;
      return;
    }
  }
}
void dfs2(int u, int fat) {
  if (edge[u].size() == 1 && !tag[u]) {
    leaf.push(u);
    return;
  }
  for (int i = 0; i < edge[u].size(); i++) {
    int now = edge[u][i];
    if (now != fat) {
      fa[now] = u;
      dis[now] = dis[u] + 1;
      if (!tag[now]) son[u]++;
      dfs2(now, u);
    }
  }
}
void clearing() {
  memset(vis, 0, sizeof(vis));
  memset(dis, 0, sizeof(dis));
}
void dijkstral(int Begin) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(make_pair(0, Begin));
  disl[Begin] = 0;
  vis[Begin] = 1;
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (int i = 0; i < edge[u].size(); i++) {
      int now = edge[u][i];
      if (disl[now] > disl[u] + 1) {
        disl[now] = disl[u] + 1;
        if (!vis[now]) {
          q.push(make_pair(disl[now], now));
        }
      }
    }
  }
}
void dijkstrar(int Begin) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(make_pair(0, Begin));
  disr[Begin] = 0;
  vis[Begin] = 1;
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (int i = 0; i < edge[u].size(); i++) {
      int now = edge[u][i];
      if (disr[now] > disr[u] + 1) {
        disr[now] = disr[u] + 1;
        if (!vis[now]) {
          q.push(make_pair(disr[now], now));
        }
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  memset(disl, 0x3f, sizeof(disl));
  memset(disr, 0x3f, sizeof(disr));
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  bfs(1);
  for (int i = 1; i <= n; i++)
    if (dis[i] > maxdis) {
      maxdis = dis[i];
      pointleft = i;
    }
  clearing();
  maxdis = 0;
  bfs(pointleft);
  for (int i = 1; i <= n; i++)
    if (dis[i] > maxdis) {
      maxdis = dis[i];
      pointright = i;
    }
  clearing();
  vis[pointleft] = 1;
  dfs(pointleft);
  clearing();
  for (int i = 1; i <= n; i++) {
    if (tag[i]) {
      rt = i;
      break;
    }
  }
  dfs2(rt, 0);
  clearing();
  dijkstral(pointleft);
  clearing();
  dijkstrar(pointright);
  clearing();
  int cnt = 0;
  while (!leaf.empty()) {
    int u = leaf.front();
    leaf.pop();
    son[fa[u]]--;
    b[++cnt] = u;
    c[cnt] = u;
    if (disl[u] > disr[u]) {
      a[cnt] = pointleft;
      ans += disl[u];
    } else {
      a[cnt] = pointright;
      ans += disr[u];
    }
    if (!tag[fa[u]] && !son[fa[u]]) {
      leaf.push(fa[u]);
    }
  }
  for (int i = 1; i < tt; i++) {
    c[++cnt] = road[i];
    b[cnt] = road[i];
    a[cnt] = pointleft;
    ans += tt - i;
  }
  printf("%lld\n", ans);
  for (int i = 1; i <= cnt; i++) {
    printf("%d %d %d\n", a[i], b[i], c[i]);
  }
  return 0;
}