#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> G[maxn];
int fa[maxn];
int n;
const int inf = 1e9;
void bfs(int s, int *d) {
  for (int i = 1; i <= n; i++) d[i] = inf;
  d[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : G[u]) {
      if (d[v] != inf) continue;
      fa[v] = u;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
}
int d1[maxn], d2[maxn], in_path[maxn];
vector<pair<int, int> > ver;
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  bfs(1, d1);
  int s = max_element(d1 + 1, d1 + 1 + n) - d1;
  bfs(s, d1);
  int t = max_element(d1 + 1, d1 + 1 + n) - d1;
  bfs(t, d2);
  vector<int> path;
  int u = s;
  while (u != t) {
    path.push_back(u);
    in_path[u] = 1;
    u = fa[u];
  }
  in_path[t] = 1;
  path.push_back(t);
  long long ans = 0;
  vector<tuple<int, int, int> > out;
  for (int i = 1; i <= n; i++)
    if (!in_path[i]) {
      ver.push_back({max(d1[i], d2[i]), i});
    }
  sort(ver.begin(), ver.end());
  reverse(ver.begin(), ver.end());
  for (auto pr : ver) {
    int u = pr.second;
    ans += pr.first;
    if (pr.first == d1[u]) {
      out.push_back(make_tuple(u, s, u));
    } else {
      out.push_back(make_tuple(u, t, u));
    }
  }
  for (int i = 0; i + 1 < path.size(); i++) {
    int u = path[i];
    ans += d2[u];
    out.push_back(make_tuple(u, t, u));
  }
  cout << ans << endl;
  for (auto tp : out) {
    printf("%d %d %d\n", get<0>(tp), get<1>(tp), get<2>(tp));
  }
  return 0;
}
