#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> G[maxn];
vector<pair<int, int>> a;
int dep[maxn], f[maxn], in[maxn];
int dia, d;
long long ans;
void dfs(int u, int fa) {
  dep[u] = dep[f[u] = fa] + 1;
  if (dep[u] > dia) dia = dep[u], d = u;
  for (auto i : G[u])
    if (i - fa) dfs(i, u);
}
void dfs2(int u, int fa, int dis, int flg = 0) {
  in[u] = 1;
  for (auto i : G[u])
    if (!in[i]) dfs2(i, fa, dis + 1, 1);
  if (flg) ans += dis, a.push_back(make_pair(fa, u));
}
int main() {
  int n, x, y;
  scanf("%d", &n);
  for (int i = 1; i < n; ++i)
    scanf("%d%d", &x, &y), G[x].push_back(y), G[y].push_back(x);
  dfs(1, 0);
  x = d;
  dia = 0, dfs(x, 0);
  y = d;
  ans = (long long)(dia - 1) * dia / 2;
  for (int i = y; i; i = f[i]) in[i] = 1;
  for (int i = y; i; i = f[i])
    if (dep[i] - 1 > dep[y] - dep[i])
      dfs2(i, x, dep[i] - 1);
    else
      dfs2(i, y, dep[y] - dep[i]);
  printf("%lld\n", ans);
  for (auto i : a) printf("%d %d %d\n", i.first, i.second, i.second);
  for (int i = y; i - x; i = f[i]) printf("%d %d %d\n", x, i, i);
}
