#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, a, b, dA[N], dB[N], deg[N];
vector<int> g[N];
int mx = -1, mxNode = -1;
void dfs(int node, int p, int dist, int d[]) {
  if (dist > mx) mx = dist, mxNode = node;
  d[node] = dist;
  for (int ch : g[node])
    if (ch != p) dfs(ch, node, dist + 1, d);
}
struct node {
  int a, b, c;
};
vector<node> ans;
int main() {
  scanf("%d", &n);
  for (int(i) = (2); (i) <= (int)(n); (i)++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b), g[b].push_back(a);
    ++deg[a], ++deg[b];
  }
  mx = -1, mxNode = -1;
  dfs(1, -1, 0, dA);
  a = mxNode;
  mx = -1, mxNode = -1;
  dfs(a, -1, 0, dA);
  b = mxNode;
  dfs(b, -1, 0, dB);
  queue<int> q;
  for (int(i) = (1); (i) <= (int)(n); (i)++)
    if (i != a && i != b && deg[i] == 1) q.push(i);
  long long ansSum = 0;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    if (dA[cur] > dB[cur])
      ans.push_back({cur, a, cur}), ansSum += dA[cur];
    else
      ans.push_back({cur, b, cur}), ansSum += dB[cur];
    for (int ch : g[cur])
      if (--deg[ch] == 1 && ch != a && ch != b) q.push(ch);
  }
  q.push(b);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    ansSum += dA[cur];
    ans.push_back({cur, a, cur});
    for (int ch : g[cur])
      if (--deg[ch] == 1 && ch != a) q.push(ch);
  }
  printf("%lld\n", ansSum);
  for (node x : ans) printf("%d %d %d\n", x.a, x.b, x.c);
  return 0;
}
