#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& os, vector<long long>& x) {
  for (long long i = 0; i < x.size(); i++) os << x[i] << ' ';
  return os;
}
ostream& operator<<(ostream& os, pair<long long, long long> x) {
  cout << x.first << ' ' << x.second << ' ';
  return os;
}
ostream& operator<<(ostream& os, vector<pair<long long, long long> >& x) {
  for (long long i = 0; i < x.size(); i++) os << x[i] << '\n';
  return os;
}
struct data {
  long long u, v, del;
  data(long long _u, long long _v, long long _del) : u(_u), v(_v), del(_del){};
};
ostream& operator<<(ostream& os, data x) {
  cout << x.u << ' ' << x.v << ' ' << x.del << '\n';
  return os;
}
ostream& operator<<(ostream& os, vector<data>& x) {
  for (long long i = 0; i < x.size(); i++) os << x[i];
  return os;
}
long long n;
vector<vector<long long> > a(200005);
pair<long long, long long> hLo[200005];
pair<long long, long long> hHi[200005];
long long depth[200005];
pair<long long, long long> dualRoot;
long long ans = 0;
vector<data> order;
bool mark[200005];
void pdfs(long long u, long long p) {
  hLo[u] = make_pair(0, u);
  hHi[u] = make_pair(0, u);
  for (auto v : a[u]) {
    if (v == p) continue;
    depth[v] = depth[u] + 1;
    pdfs(v, u);
    hLo[u] = max(hLo[u], make_pair(hLo[v].first + 1, hLo[v].second));
    hHi[u] = max(hHi[u], make_pair(hHi[v].first + 1, v));
  }
}
void dfs2(long long u, long long p, pair<long long, long long> upLen,
          long long downLen) {
  if (u == 1) {
    upLen = make_pair(0, 1);
    for (auto v : a[u]) {
      if (v == p || mark[v]) continue;
      upLen = max(upLen, make_pair(hLo[v].first + 1, hLo[v].second));
    }
    downLen = depth[dualRoot.second];
    dfs2(hHi[1].second, 1, make_pair(upLen.first + 1, upLen.second),
         downLen - 1);
    return;
  }
  pair<long long, long long> mx = upLen;
  for (auto v : a[u]) {
    if (v == p) continue;
    if (mark[v]) continue;
    mx = max(mx, make_pair(hLo[v].first + 1, hLo[v].second));
  }
  for (auto v : a[u]) {
    if (v == p) continue;
    if (!mark[v]) continue;
    dfs2(v, u, make_pair(mx.first + 1, mx.second),
         downLen + (mark[v] ? -1 : +1));
  }
  for (auto v : a[u]) {
    if (v == p) continue;
    if (mark[v]) continue;
    dfs2(v, u, make_pair(upLen.first + 1, upLen.second),
         downLen + (mark[v] ? -1 : +1));
  }
  if (!mark[u]) {
    if (upLen.first > downLen) {
      ans += upLen.first;
      order.push_back(data(upLen.second, u, u));
    } else {
      ans += downLen;
      order.push_back(data(dualRoot.second, u, u));
    }
  }
}
void dfs1(long long u, long long p) {
  for (auto v : a[u]) {
    if (v == p) continue;
    if (u == 1 && v == hHi[1].second) continue;
    dfs1(v, u);
  }
  if (u != 1) {
    ans += depth[u] + depth[dualRoot.second];
    order.push_back(data(u, dualRoot.second, u));
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    a[u].push_back(v);
    a[v].push_back(u);
  }
  pdfs(1, 1);
  dualRoot.first = 1;
  long long curVer = 1, curH = hHi[1].first;
  while (curH > 0) {
    mark[curVer] = 1;
    curH--;
    curVer = hHi[curVer].second;
    mark[curVer] = 1;
  }
  dualRoot.second = curVer;
  dfs2(1, 1, make_pair(0, 0), 0);
  dfs1(1, 1);
  curVer = 1, curH = hHi[1].first;
  while (curH > 0) {
    order.push_back(data(curVer, dualRoot.second, curVer));
    ans += curH;
    curH--;
    curVer = hHi[curVer].second;
  }
  cout << ans << '\n';
  cout << order << '\n';
}
