#include <bits/stdc++.h>
using namespace std;
namespace Acc {
const int N = 2e5 + 10;
basic_string<int> G[N];
int n, st, bg, l, ed, f[N], dis[N], pos, tag[N], stk[2][N], cnt;
long long ans;
void dfs(int u, int fa, int d) {
  if (dis[u] = st ? d : 0, f[u] = fa, d > l) l = d, pos = u;
  for (auto v : G[u])
    if (v != fa) dfs(v, u, d + 1);
}
void print(int u, int fa, int d) {
  for (auto v : G[u])
    if (v != fa) print(v, u, d + 1);
  if (!tag[u])
    ans += max(dis[u], d), cnt++, stk[0][cnt] = u,
                                  stk[1][cnt] = dis[u] > d ? bg : ed;
  else
    ans += d;
}
void work() {
  cin >> n;
  for (int i = 1, x, y; i < n; i++) cin >> x >> y, G[x] += y, G[y] += x;
  dfs(1, 0, 0), bg = pos, st = 1, l = 0, dfs(bg, 0, 0), ed = pos, tag[bg] = 1;
  for (int u = ed; u != bg; u = f[u]) tag[u] = 1;
  print(ed, 0, 0), cout << ans << endl;
  for (int i = 1; i <= cnt; i++)
    cout << stk[0][i] << ' ' << stk[1][i] << ' ' << stk[0][i] << '\n';
  for (int u = ed; u != bg; u = f[u])
    cout << u << ' ' << bg << ' ' << u << '\n';
}
}  // namespace Acc
int main() { return Acc::work(), 0; }
