#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
vector<int> path;
vector<int> G[N];
int dis[N];
int fa[N][22];
int inpath[N];
int L[N];
int tot = 0;
int Max_Len = 0;
int root_u, root_v;
void init() {
  Max_Len = 0;
  path.clear();
  tot = 0;
  memset(inpath, 0, sizeof(inpath));
  for (int i = 0; i < N; i++) G[i].clear();
}
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
void init_lca(int u, int f, int d) {
  dis[u] = d;
  fa[u][0] = f;
  if (dis[u] >= Max_Len) Max_Len = dis[u], root_u = u;
  for (int i = 1; i < 22; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (auto v : G[u]) {
    if (v == f) continue;
    init_lca(v, u, d + 1);
  }
}
int lca(int u, int v) {
  if (dis[u] < dis[v]) swap(u, v);
  for (int i = 21; i >= 0; i--)
    if (dis[fa[u][i]] >= dis[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = 21; i >= 0; i--)
    if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
void find_root(int u, int fa, int d) {
  L[u] = ++tot;
  if (d >= Max_Len) root_v = u, Max_Len = d;
  for (auto v : G[u]) {
    if (v == fa) continue;
    find_root(v, u, d + 1);
  }
}
bool find_path(int u, int fa) {
  if (u == root_v) {
    inpath[u] = 1;
    path.push_back(u);
    return true;
  }
  for (auto v : G[u]) {
    if (v == fa) continue;
    if (find_path(v, u)) {
      inpath[u] = 1;
      path.push_back(u);
      return true;
    }
  }
  return false;
}
vector<pair<int, int> > ans;
int main() {
  ios ::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    init();
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v;
      add_edge(u, v);
    }
    init_lca(1, 1, 0);
    find_root(root_u, root_u, 0);
    find_path(root_u, root_u);
    ans.clear();
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      if (!inpath[i]) {
        long long ans1 = dis[root_u] + dis[i] - 2 * dis[lca(root_u, i)];
        long long ans2 = dis[root_v] + dis[i] - 2 * dis[lca(root_v, i)];
        sum += max(ans1, ans2);
        if (ans1 > ans2)
          ans.push_back(make_pair(root_u, i));
        else
          ans.push_back(make_pair(root_v, i));
      }
    }
    sort(ans.begin(), ans.end(), [](pair<int, int> a, pair<int, int> b) {
      return L[a.second] > L[b.second];
    });
    for (int i = 0; i < path.size() - 1; i++) {
      sum += dis[path[i]] + dis[root_u] - 2 * dis[lca(path[i], root_u)];
      ans.push_back(make_pair(root_u, path[i]));
    }
    cout << sum << endl;
    for (auto temp : ans)
      cout << temp.first << " " << temp.second << " " << temp.second << endl;
  }
}
