#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x) {
  char c = getchar();
  x = 0;
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int N = 2e5 + 50;
struct E {
  int d, nxt;
} es[N << 1];
int n, G[N], en;
long long ans;
void add(int s, int d) {
  es[++en].d = d;
  es[en].nxt = G[s];
  G[s] = en;
}
int ds[N], dt[N], que[N], h, t, pre[N];
int sz, dia[N], dias, diat, india[N];
void clr() {
  en = 0;
  memset(G, 0, sizeof(G));
  sz = 0;
}
void bfs(int s, int *dis, size_t siz_dis) {
  memset(dis, -1, siz_dis);
  memset(pre, -1, sizeof(pre));
  pre[s] = -1;
  h = t = 0;
  que[t++] = s;
  dis[s] = 0;
  while (h != t) {
    int v = que[h++];
    for (int i = G[v]; i; i = es[i].nxt) {
      int d = es[i].d;
      if (dis[d] < 0) {
        dis[d] = dis[v] + 1;
        pre[d] = v;
        que[t++] = d;
      }
    }
  }
}
void get_diameter() {
  bfs(dias = 1, ds, sizeof(ds));
  for (int i = 1; i <= (n); ++i)
    if (ds[i] > ds[dias]) dias = i;
  bfs(dias, ds, sizeof(ds));
  diat = dias;
  for (int i = 1; i <= (n); ++i)
    if (ds[i] > ds[diat]) diat = i;
  bfs(diat, dt, sizeof(dt));
  for (int i = 1; i <= (n); ++i)
    ;
  ;
  sz = 0;
  for (int t = dias; t != -1; t = pre[t]) dia[++sz] = t;
  for (int i = 1; i <= (n); ++i)
    ;
  for (int i = 1; i <= (sz); ++i) india[dia[i]] = true;
}
int vis[N];
void print(int v) {
  vis[v] = 1;
  for (int i = G[v]; i; i = es[i].nxt) {
    if (!vis[es[i].d]) print(es[i].d);
  }
  if (!india[v]) {
    if (ds[v] >= dt[v])
      printf("%d %d %d\n", dias, v, v);
    else
      printf("%d %d %d\n", diat, v, v);
  }
}
void solve() {
  for (int i = 1; i <= (n); ++i)
    if (!india[i]) ans += max(ds[i], dt[i]);
  for (int i = 1; i <= (sz - 1); ++i) ans += dt[dia[i]];
  printf("%lld\n", ans);
  print(dias);
  for (int i = 1; i <= (sz - 1); ++i) {
    printf("%d %d %d\n", dia[i], diat, dia[i]);
  }
}
int main(int argc, char *argv[]) {
  read(n);
  for (int i = 2; i <= (n); ++i) {
    int a, b;
    read(a);
    read(b);
    add(a, b);
    add(b, a);
  }
  get_diameter();
  solve();
  return 0;
}

### Prompt

Create a solution in cpp for 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>
void read(t &x) {
  char c = getchar();
  x = 0;
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int N = 2e5 + 50;
struct E {
  int d, nxt;
} es[N << 1];
int n, G[N], en;
long long ans;
void add(int s, int d) {
  es[++en].d = d;
  es[en].nxt = G[s];
  G[s] = en;
}
int ds[N], dt[N], que[N], h, t, pre[N];
int sz, dia[N], dias, diat, india[N];
void clr() {
  en = 0;
  memset(G, 0, sizeof(G));
  sz = 0;
}
void bfs(int s, int *dis, size_t siz_dis) {
  memset(dis, -1, siz_dis);
  memset(pre, -1, sizeof(pre));
  pre[s] = -1;
  h = t = 0;
  que[t++] = s;
  dis[s] = 0;
  while (h != t) {
    int v = que[h++];
    for (int i = G[v]; i; i = es[i].nxt) {
      int d = es[i].d;
      if (dis[d] < 0) {
        dis[d] = dis[v] + 1;
        pre[d] = v;
        que[t++] = d;
      }
    }
  }
}
void get_diameter() {
  bfs(dias = 1, ds, sizeof(ds));
  for (int i = 1; i <= (n); ++i)
    if (ds[i] > ds[dias]) dias = i;
  bfs(dias, ds, sizeof(ds));
  diat = dias;
  for (int i = 1; i <= (n); ++i)
    if (ds[i] > ds[diat]) diat = i;
  bfs(diat, dt, sizeof(dt));
  for (int i = 1; i <= (n); ++i)
    ;
  ;
  sz = 0;
  for (int t = dias; t != -1; t = pre[t]) dia[++sz] = t;
  for (int i = 1; i <= (n); ++i)
    ;
  for (int i = 1; i <= (sz); ++i) india[dia[i]] = true;
}
int vis[N];
void print(int v) {
  vis[v] = 1;
  for (int i = G[v]; i; i = es[i].nxt) {
    if (!vis[es[i].d]) print(es[i].d);
  }
  if (!india[v]) {
    if (ds[v] >= dt[v])
      printf("%d %d %d\n", dias, v, v);
    else
      printf("%d %d %d\n", diat, v, v);
  }
}
void solve() {
  for (int i = 1; i <= (n); ++i)
    if (!india[i]) ans += max(ds[i], dt[i]);
  for (int i = 1; i <= (sz - 1); ++i) ans += dt[dia[i]];
  printf("%lld\n", ans);
  print(dias);
  for (int i = 1; i <= (sz - 1); ++i) {
    printf("%d %d %d\n", dia[i], diat, dia[i]);
  }
}
int main(int argc, char *argv[]) {
  read(n);
  for (int i = 2; i <= (n); ++i) {
    int a, b;
    read(a);
    read(b);
    add(a, b);
    add(b, a);
  }
  get_diameter();
  solve();
  return 0;
}

```