#include <bits/stdc++.h>
using namespace std;
vector<int> g[200000];
int d[200000], deg[200000] = {0};
void dfs(int v, int p = -1) {
  for (int u : g[v]) {
    if (u != p) d[u] = d[v] + 1, dfs(u, v);
  }
}
vector<tuple<int, int, int>> ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a, b, n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i)
    cin >> a >> b, --a, --b, g[a].push_back(b), g[b].push_back(a), ++deg[a],
        ++deg[b];
  d[0] = 0;
  dfs(0);
  int u = max_element(d, d + n) - d;
  d[u] = 0;
  dfs(u);
  vector<int> du(d, d + n);
  int v = max_element(d, d + n) - d;
  d[v] = 0;
  dfs(v);
  vector<int> dv(d, d + n);
  queue<int> leaves;
  for (int i = 0; i < n; ++i)
    if (deg[i] == 1 and i != v and i != u) {
      leaves.push(i);
    }
  long long dis = 0;
  while (leaves.size()) {
    int l = leaves.front();
    leaves.pop();
    --deg[l];
    if (dv[l] > du[l]) {
      dis += dv[l];
      ans.push_back({v, l, l});
    } else {
      dis += du[l];
      ans.push_back({u, l, l});
    }
    for (int u : g[l]) {
      if (deg[u] == 0) continue;
      --deg[u];
      if (deg[u] == 1) leaves.push(u);
    }
  }
  dis += (long long)dv[u] * (dv[u] + 1) / 2;
  cout << dis << '\n';
  for (auto V : ans) {
    int c = 0;
    tie(a, b, c) = V;
    cout << a + 1 << ' ' << b + 1 << ' ' << c + 1 << '\n';
  }
  while (u != v) {
    cout << u + 1 << ' ' << v + 1 << ' ' << u + 1 << '\n';
    for (int k : g[u]) {
      if (dv[k] < dv[u]) {
        u = k;
        break;
      }
    }
  }
}
