#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
int n;
vector<int> g[N];
int mx, gag, H;
int gag1, gag2;
bool used[N];
int parent[N];
void dfs(int v, int r, int p) {
  if (r > mx) {
    mx = r;
    gag = v;
  }
  for (int i = 0; i < (int)g[v].size(); i++) {
    int to = g[v][i];
    if (to == p) continue;
    parent[to] = v;
    dfs(to, r + 1, v);
  }
}
int dp[N][22];
int tin[N], tout[N], tm;
int deg[N];
vector<int> vertex[N];
void rec(int v, int r, int p) {
  if (used[v] == false) vertex[r].push_back(v);
  if (r > H) H = r;
  tin[v] = tm++;
  deg[v] = r;
  dp[v][0] = parent[v];
  for (int i = 1; i <= 20; i++) dp[v][i] = dp[dp[v][i - 1]][i - 1];
  for (int i = 0; i < (int)g[v].size(); i++) {
    int to = g[v][i];
    if (to == p) continue;
    parent[to] = v;
    rec(to, r + 1, v);
  }
  tout[v] = tm++;
}
bool check(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); }
int lca(int a, int b) {
  if (check(a, b)) return a;
  if (check(b, a)) return b;
  for (int i = 20; i >= 0; i--) {
    if (check(dp[a][i], b) == false) {
      a = dp[a][i];
    }
  }
  return parent[a];
}
vector<int> answ[5];
long long sum = 0;
int dist(int a, int b) {
  if (a == b) return 0;
  int p = lca(a, b);
  return deg[a] + deg[b] - 2 * deg[p];
}
void upd(int v) {
  int d1 = dist(v, gag1);
  int d2 = dist(v, gag2);
  if (d1 > d2) {
    sum += 1ll * d1;
    answ[1].push_back(gag1);
  } else {
    sum += 1ll * d2;
    answ[1].push_back(gag2);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1, 0, -1);
  mx = 0;
  gag1 = gag;
  dfs(gag, 0, -1);
  gag2 = gag;
  used[gag] = true;
  while (gag != gag1) {
    gag = parent[gag];
    used[gag] = true;
  }
  parent[gag1] = gag1;
  rec(gag1, 0, -1);
  for (int i = H; i >= 0; i--) {
    for (int j = 0; j < (int)vertex[i].size(); j++) {
      int v = vertex[i][j];
      answ[0].push_back(v);
      upd(v);
      answ[2].push_back(v);
      used[v] = true;
    }
  }
  gag = gag2;
  while (gag != gag1) {
    answ[0].push_back(gag);
    answ[1].push_back(gag1);
    answ[2].push_back(gag);
    sum += 1ll * mx;
    mx--;
    gag = parent[gag];
  }
  cout << sum << endl;
  for (int i = 0; i < n - 1; i++) {
    printf("%d %d %d\n", answ[0][i], answ[1][i], answ[2][i]);
  }
  return 0;
}
