#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[200005], diam[200005], tour, path;
long long int n, h[200005], first[200005], stmin[2 * 200005][20], root;
long long int vis1[200005], vis2[200005], vis3[200005], vis4[200005];
long long int parent[200005], dist[200005], root1;
void height_initialise(long long int x, long long int y) {
  h[x] = y;
  vis1[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int node = v[x][i];
    if (vis1[node] == 0) {
      height_initialise(node, y + 1);
    }
  }
}
void distanc(long long int x, long long int y) {
  dist[x] = y;
  vis3[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int node = v[x][i];
    if (vis3[node] == 0) {
      distanc(node, y + 1);
    }
  }
}
void euler(long long int x, long long int y) {
  vis2[x] = 1;
  tour.push_back(x);
  for (int i = 0; i < v[x].size(); i++) {
    int node = v[x][i];
    if (vis2[node] == 0) {
      euler(node, x);
    }
  }
  if (y != 0) tour.push_back(y);
}
void euler_tour() {
  euler(root, 0);
  for (int i = 0; i < tour.size(); i++) {
    if (first[tour[i]] == 0) {
      first[tour[i]] = i;
    }
  }
}
void sparsemin() {
  long long int i, j, x, y;
  for (i = 0; i < tour.size(); i++) {
    stmin[i][0] = tour[i];
  }
  for (j = 1; (1 << j) <= tour.size(); j++) {
    for (i = 0; i <= tour.size() - (1 << j); i++) {
      x = stmin[i][j - 1];
      y = stmin[i + (1 << (j - 1))][j - 1];
      if (h[x] > h[y]) {
        stmin[i][j] = y;
      } else {
        stmin[i][j] = x;
      }
    }
  }
}
long long int spminquery(long long int l, long long int r) {
  long long int length, k, x, y;
  length = r - l + 1;
  k = log2(length);
  x = stmin[l][k];
  y = stmin[l + length - (1 << k)][k];
  if (h[x] < h[y]) {
    return x;
  }
  return y;
}
int bfs(int x, int flag) {
  int i, y;
  queue<int> q;
  q.push(x);
  int vis[200005] = {0};
  while (q.size()) {
    x = q.front();
    q.pop();
    vis[x] = 1;
    for (int i = 0; i < v[x].size(); i++) {
      int node = v[x][i];
      if (vis[node] == 0) {
        q.push(node);
        if (flag == 1) {
          parent[node] = x;
        }
      }
    }
    y = x;
  }
  return y;
}
void diameter() {
  int x, y, z, i, j;
  x = bfs(1, 0);
  y = bfs(x, 1);
  root1 = y;
  z = y;
  while (parent[z] != 0) {
    path.push_back(z);
    z = parent[z];
  }
  path.push_back(z);
  reverse(path.begin(), path.end());
  for (i = 0; i < path.size(); i++) {
    vis4[path[i]] = 1;
    diam[i].push_back(path[i]);
  }
  root = x;
  for (i = 0; i < path.size(); i++) {
    queue<int> q;
    q.push(path[i]);
    while (q.size()) {
      x = q.front();
      q.pop();
      vis4[x] = 1;
      for (j = 0; j < v[x].size(); j++) {
        int node = v[x][j];
        if (vis4[node] == 0) {
          q.push(node);
          diam[i].push_back(node);
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  long long int i, j, x, y, z, l, r, q, ans = 0;
  vector<pair<long long int, long long int> > an;
  for (i = 0; i < n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  diameter();
  height_initialise(root, 0);
  euler_tour();
  sparsemin();
  distanc(root, 0);
  for (i = 0; i < path.size() / 2; i++) {
    for (j = diam[i].size() - 1; j > 0; j--) {
      x = root1;
      y = diam[i][j];
      an.push_back(make_pair(x, y));
      l = first[x];
      r = first[y];
      z = spminquery(min(l, r), max(l, r));
      z = dist[x] + dist[y] - 2 * dist[z];
      ans += z;
    }
  }
  x = i;
  for (i = x; i < path.size(); i++) {
    for (j = diam[i].size() - 1; j > 0; j--) {
      x = root;
      y = diam[i][j];
      an.push_back(make_pair(x, y));
      l = first[x];
      r = first[y];
      z = spminquery(min(l, r), max(l, r));
      z = dist[x] + dist[y] - 2 * dist[z];
      ans += z;
    }
  }
  for (i = path.size() - 1; i > 0; i--) {
    x = root;
    y = diam[i][0];
    an.push_back(make_pair(x, y));
    l = first[x];
    r = first[y];
    z = spminquery(min(l, r), max(l, r));
    z = dist[x] + dist[y] - 2 * dist[z];
    ans += z;
  }
  cout << ans << endl;
  for (i = 0; i < an.size(); i++) {
    cout << an[i].first << " " << an[i].second << " " << an[i].second << "\n";
  }
  return 0;
}

### Prompt

Please create a solution in Cpp 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;
vector<long long int> v[200005], diam[200005], tour, path;
long long int n, h[200005], first[200005], stmin[2 * 200005][20], root;
long long int vis1[200005], vis2[200005], vis3[200005], vis4[200005];
long long int parent[200005], dist[200005], root1;
void height_initialise(long long int x, long long int y) {
  h[x] = y;
  vis1[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int node = v[x][i];
    if (vis1[node] == 0) {
      height_initialise(node, y + 1);
    }
  }
}
void distanc(long long int x, long long int y) {
  dist[x] = y;
  vis3[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int node = v[x][i];
    if (vis3[node] == 0) {
      distanc(node, y + 1);
    }
  }
}
void euler(long long int x, long long int y) {
  vis2[x] = 1;
  tour.push_back(x);
  for (int i = 0; i < v[x].size(); i++) {
    int node = v[x][i];
    if (vis2[node] == 0) {
      euler(node, x);
    }
  }
  if (y != 0) tour.push_back(y);
}
void euler_tour() {
  euler(root, 0);
  for (int i = 0; i < tour.size(); i++) {
    if (first[tour[i]] == 0) {
      first[tour[i]] = i;
    }
  }
}
void sparsemin() {
  long long int i, j, x, y;
  for (i = 0; i < tour.size(); i++) {
    stmin[i][0] = tour[i];
  }
  for (j = 1; (1 << j) <= tour.size(); j++) {
    for (i = 0; i <= tour.size() - (1 << j); i++) {
      x = stmin[i][j - 1];
      y = stmin[i + (1 << (j - 1))][j - 1];
      if (h[x] > h[y]) {
        stmin[i][j] = y;
      } else {
        stmin[i][j] = x;
      }
    }
  }
}
long long int spminquery(long long int l, long long int r) {
  long long int length, k, x, y;
  length = r - l + 1;
  k = log2(length);
  x = stmin[l][k];
  y = stmin[l + length - (1 << k)][k];
  if (h[x] < h[y]) {
    return x;
  }
  return y;
}
int bfs(int x, int flag) {
  int i, y;
  queue<int> q;
  q.push(x);
  int vis[200005] = {0};
  while (q.size()) {
    x = q.front();
    q.pop();
    vis[x] = 1;
    for (int i = 0; i < v[x].size(); i++) {
      int node = v[x][i];
      if (vis[node] == 0) {
        q.push(node);
        if (flag == 1) {
          parent[node] = x;
        }
      }
    }
    y = x;
  }
  return y;
}
void diameter() {
  int x, y, z, i, j;
  x = bfs(1, 0);
  y = bfs(x, 1);
  root1 = y;
  z = y;
  while (parent[z] != 0) {
    path.push_back(z);
    z = parent[z];
  }
  path.push_back(z);
  reverse(path.begin(), path.end());
  for (i = 0; i < path.size(); i++) {
    vis4[path[i]] = 1;
    diam[i].push_back(path[i]);
  }
  root = x;
  for (i = 0; i < path.size(); i++) {
    queue<int> q;
    q.push(path[i]);
    while (q.size()) {
      x = q.front();
      q.pop();
      vis4[x] = 1;
      for (j = 0; j < v[x].size(); j++) {
        int node = v[x][j];
        if (vis4[node] == 0) {
          q.push(node);
          diam[i].push_back(node);
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  long long int i, j, x, y, z, l, r, q, ans = 0;
  vector<pair<long long int, long long int> > an;
  for (i = 0; i < n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  diameter();
  height_initialise(root, 0);
  euler_tour();
  sparsemin();
  distanc(root, 0);
  for (i = 0; i < path.size() / 2; i++) {
    for (j = diam[i].size() - 1; j > 0; j--) {
      x = root1;
      y = diam[i][j];
      an.push_back(make_pair(x, y));
      l = first[x];
      r = first[y];
      z = spminquery(min(l, r), max(l, r));
      z = dist[x] + dist[y] - 2 * dist[z];
      ans += z;
    }
  }
  x = i;
  for (i = x; i < path.size(); i++) {
    for (j = diam[i].size() - 1; j > 0; j--) {
      x = root;
      y = diam[i][j];
      an.push_back(make_pair(x, y));
      l = first[x];
      r = first[y];
      z = spminquery(min(l, r), max(l, r));
      z = dist[x] + dist[y] - 2 * dist[z];
      ans += z;
    }
  }
  for (i = path.size() - 1; i > 0; i--) {
    x = root;
    y = diam[i][0];
    an.push_back(make_pair(x, y));
    l = first[x];
    r = first[y];
    z = spminquery(min(l, r), max(l, r));
    z = dist[x] + dist[y] - 2 * dist[z];
    ans += z;
  }
  cout << ans << endl;
  for (i = 0; i < an.size(); i++) {
    cout << an[i].first << " " << an[i].second << " " << an[i].second << "\n";
  }
  return 0;
}

```