#include <bits/stdc++.h>
using namespace std;
long long n;
vector<int> g[200020];
int d1[200020], d2[200020];
int deg[200020];
bool vis[200020];
vector<pair<pair<long long, long long>, long long> > resb;
pair<long long, long long> dfs(long long v, long long pre, int d[],
                               long long nd) {
  pair<long long, long long> res = pair<long long, long long>(0, v);
  d[v] = nd;
  for (long long nv : g[v]) {
    if (nv == pre) continue;
    pair<long long, long long> tmp = dfs(nv, v, d, nd + 1);
    tmp.first++;
    res = (res > tmp ? res : tmp);
  }
  return res;
}
int main() {
  cin >> n;
  for (long long i = (long long)(0); i < (long long)(n - 1); i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  long long u, v;
  pair<long long, long long> tmp = dfs(0, -1, d1, 0);
  u = tmp.second;
  tmp = dfs(u, -1, d1, 0);
  v = tmp.second;
  dfs(v, -1, d2, 0);
  for (long long i = (long long)(0); i < (long long)(n); i++)
    deg[i] = g[i].size();
  priority_queue<pair<long long, long long> > que;
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    if (i == u || i == v) continue;
    if (deg[i] == 1) {
      que.push(pair<long long, long long>(max(d1[i], d2[i]), i));
    }
  }
  long long resa = 0;
  while (que.size()) {
    pair<long long, long long> crt = que.top();
    que.pop();
    resa += crt.first;
    if (d1[crt.second] > d2[crt.second])
      resb.push_back(pair<pair<long long, long long>, long long>(
          pair<long long, long long>(crt.second + 1, u + 1), crt.second + 1));
    else
      resb.push_back(pair<pair<long long, long long>, long long>(
          pair<long long, long long>(crt.second + 1, v + 1), crt.second + 1));
    vis[crt.second] = true;
    for (long long nv : g[crt.second]) {
      deg[nv]--;
      if (deg[nv] == 1) {
        que.push(pair<long long, long long>(max(d1[nv], d2[nv]), nv));
      }
    }
  }
  long long crt = u;
  while (crt != v) {
    resa += d2[crt];
    resb.push_back(pair<pair<long long, long long>, long long>(
        pair<long long, long long>(crt + 1, v + 1), crt + 1));
    vis[crt] = true;
    for (long long nv : g[crt]) {
      if (!vis[nv]) {
        crt = nv;
        break;
      }
    }
  }
  cout << resa << endl;
  for (long long i = (long long)(0); i < (long long)(resb.size()); i++)
    cout << resb[i].first.first << " " << resb[i].first.second << " "
         << resb[i].second << endl;
  return 0;
}
