#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long MOD = 1000000007;
const int INF = 1e9;
const long long LINF = 1LL * INF * INF;
const int MAXN = 200007;
const double EPS = 1e-7;
vector<int> G[MAXN];
bool used[MAXN], Del[MAXN];
int D1[MAXN], D2[MAXN], v1, v2;
long long s = 0;
vector<pair<int, int> > Ans;
void dfs1(int v, int *D) {
  used[v] = true;
  for (int i = (0); i < ((int)((G[v]).size())); ++i) {
    int to = G[v][i];
    if (!used[to]) {
      D[to] = D[v] + 1;
      dfs1(to, D);
    }
  }
}
bool dfs2(int v, bool d) {
  used[v] = true;
  Del[v] = v != v1 && (d || v != v2);
  for (int i = (0); i < ((int)((G[v]).size())); ++i) {
    int to = G[v][i];
    if (!used[to] && !Del[to] && !dfs2(to, d)) Del[v] = false;
  }
  if (Del[v]) {
    pair<int, int> p;
    if (D1[v] > D2[v] || d) {
      p.first = v1;
      s += D1[v];
    } else {
      p.first = v2;
      s += D2[v];
    }
    p.second = v;
    Ans.push_back(p);
  }
  return Del[v];
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = (0); i < (n - 1); ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  D2[0] = 0;
  dfs1(0, D2);
  for (int i = (0); i < (n); ++i)
    if (D2[i] > D2[v1]) v1 = i;
  memset(used, 0, MAXN);
  D1[v1] = 0;
  dfs1(v1, D1);
  for (int i = (0); i < (n); ++i)
    if (D1[i] > D1[v2]) v2 = i;
  memset(used, 0, MAXN);
  D2[v2] = 0;
  dfs1(v2, D2);
  memset(used, 0, MAXN);
  dfs2(v1, 0);
  memset(used, 0, MAXN);
  dfs2(v1, 1);
  cout << s << '\n';
  for (int i = (0); i < (n - 1); ++i)
    cout << Ans[i].first + 1 << ' ' << Ans[i].second + 1 << ' '
         << Ans[i].second + 1 << '\n';
  return 0;
}
