#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  char ch = getchar();
  bool f = 0;
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (((x << 2) + x) << 1) + ch - 48;
    ch = getchar();
  }
  if (f) x = -x;
}
int N;
long long ans;
struct node {
  int nex, to;
} edge[200005 << 1];
int head[200005], tot;
void insert(int from, int to) {
  ++tot;
  edge[tot].nex = head[from];
  head[from] = tot;
  edge[tot].to = to;
}
void build() {
  int from, to, dis;
  for (int i = 1; i < N; i++) {
    read(from);
    read(to);
    insert(from, to);
    insert(to, from);
  }
}
int dis_1[200005], dis_2[200005], end_point_1, end_point_2;
void dfs_1(int father, int from, int dis) {
  dis_1[from] = dis;
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_1(from, edge[i].to, dis + 1);
  }
}
void dfs_2(int father, int from, int dis) {
  dis_2[from] = dis;
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_2(from, edge[i].to, dis + 1);
  }
}
bool mark[200005], flag;
void dfs_mark(int father, int from, int end_point) {
  if (from == end_point) {
    mark[from] = 1;
    flag = 1;
    return;
  }
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_mark(from, edge[i].to, end_point);
    if (flag == 1) {
      mark[from] = 1;
      return;
    }
  }
}
void find_diameter() {
  dfs_1(0, 1, 0);
  int lenth = 0;
  for (int i = 1; i <= N; i++) {
    if (dis_1[i] > lenth) {
      lenth = dis_1[i];
      end_point_1 = i;
    }
  }
  dfs_1(0, end_point_1, 0);
  lenth = 0;
  for (int i = 1; i <= N; i++) {
    if (dis_1[i] > lenth) {
      lenth = dis_1[i];
      end_point_2 = i;
    }
  }
  dfs_2(0, end_point_2, 0);
}
int from_[200005], end_[200005], t;
void dfs_greedy(int father, int from) {
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_greedy(from, edge[i].to);
  }
  if (!mark[from]) {
    if (dis_1[from] >= dis_2[from]) {
      ans += dis_1[from];
      from_[++t] = from;
      end_[t] = end_point_1;
    } else {
      ans += dis_2[from];
      from_[++t] = from;
      end_[t] = end_point_2;
    }
  }
}
void dfs_endy(int father, int from) {
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father && mark[edge[i].to]) dfs_endy(from, edge[i].to);
  }
  ans += dis_1[from];
  from_[++t] = from;
  end_[t] = end_point_1;
}
void greedy() {
  dfs_greedy(0, end_point_1);
  dfs_endy(0, end_point_1);
}
int main() {
  read(N);
  build();
  find_diameter();
  dfs_mark(0, end_point_1, end_point_2);
  greedy();
  cout << ans << endl;
  for (int i = 1; i < N; i++) {
    cout << from_[i] << " " << end_[i] << " " << from_[i] << endl;
  }
}

### Prompt

Construct a cpp code solution to the problem outlined:

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;
inline void read(int &x) {
  x = 0;
  char ch = getchar();
  bool f = 0;
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (((x << 2) + x) << 1) + ch - 48;
    ch = getchar();
  }
  if (f) x = -x;
}
int N;
long long ans;
struct node {
  int nex, to;
} edge[200005 << 1];
int head[200005], tot;
void insert(int from, int to) {
  ++tot;
  edge[tot].nex = head[from];
  head[from] = tot;
  edge[tot].to = to;
}
void build() {
  int from, to, dis;
  for (int i = 1; i < N; i++) {
    read(from);
    read(to);
    insert(from, to);
    insert(to, from);
  }
}
int dis_1[200005], dis_2[200005], end_point_1, end_point_2;
void dfs_1(int father, int from, int dis) {
  dis_1[from] = dis;
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_1(from, edge[i].to, dis + 1);
  }
}
void dfs_2(int father, int from, int dis) {
  dis_2[from] = dis;
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_2(from, edge[i].to, dis + 1);
  }
}
bool mark[200005], flag;
void dfs_mark(int father, int from, int end_point) {
  if (from == end_point) {
    mark[from] = 1;
    flag = 1;
    return;
  }
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_mark(from, edge[i].to, end_point);
    if (flag == 1) {
      mark[from] = 1;
      return;
    }
  }
}
void find_diameter() {
  dfs_1(0, 1, 0);
  int lenth = 0;
  for (int i = 1; i <= N; i++) {
    if (dis_1[i] > lenth) {
      lenth = dis_1[i];
      end_point_1 = i;
    }
  }
  dfs_1(0, end_point_1, 0);
  lenth = 0;
  for (int i = 1; i <= N; i++) {
    if (dis_1[i] > lenth) {
      lenth = dis_1[i];
      end_point_2 = i;
    }
  }
  dfs_2(0, end_point_2, 0);
}
int from_[200005], end_[200005], t;
void dfs_greedy(int father, int from) {
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father) dfs_greedy(from, edge[i].to);
  }
  if (!mark[from]) {
    if (dis_1[from] >= dis_2[from]) {
      ans += dis_1[from];
      from_[++t] = from;
      end_[t] = end_point_1;
    } else {
      ans += dis_2[from];
      from_[++t] = from;
      end_[t] = end_point_2;
    }
  }
}
void dfs_endy(int father, int from) {
  for (int i = head[from]; i; i = edge[i].nex) {
    if (edge[i].to != father && mark[edge[i].to]) dfs_endy(from, edge[i].to);
  }
  ans += dis_1[from];
  from_[++t] = from;
  end_[t] = end_point_1;
}
void greedy() {
  dfs_greedy(0, end_point_1);
  dfs_endy(0, end_point_1);
}
int main() {
  read(N);
  build();
  find_diameter();
  dfs_mark(0, end_point_1, end_point_2);
  greedy();
  cout << ans << endl;
  for (int i = 1; i < N; i++) {
    cout << from_[i] << " " << end_[i] << " " << from_[i] << endl;
  }
}

```