#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 1e6 + 7;
const double eps = 0.00000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
  T ret = 1;
  for (; b; b >>= 1ll, a = 1ll * a * a % M)
    if (b & 1) ret = 1ll * ret * a % M;
  return ret;
}
int n, i, j, k;
vector<int> edge[maxn];
long long ans = 0;
int maxlen, node;
int f[maxn][20];
int dep[maxn];
void dfs(int x, int fa, int len) {
  f[x][0] = fa;
  int i;
  dep[x] = len;
  for (i = 1; i < 20; i++) f[x][i] = f[f[x][i - 1]][i - 1];
  if (len > maxlen) node = x, maxlen = len;
  for (int v : edge[x])
    if (v != fa) dfs(v, x, len + 1);
}
int lca(int x, int y) {
  int i;
  if (dep[x] < dep[y]) swap(x, y);
  for (i = 20 - 1; i >= 0; i--)
    if (dep[x] - dep[y] >= 1 << i) x = f[x][i];
  if (x == y) return x;
  for (i = 20 - 1; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
int dis(int x, int y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)]; }
queue<int> Q;
int d[maxn];
vector<pair<pair<int, int>, int> > ANS;
int main() {
  scanf("%d", &n);
  int u, v;
  for (i = 1; i <= n - 1; i++) {
    scanf("%d%d", &u, &v);
    edge[v].push_back(u);
    edge[u].push_back(v);
    d[u]++;
    d[v]++;
  }
  dfs(1, 0, 0);
  u = node;
  maxlen = 0;
  dfs(u, 0, 0);
  v = node;
  for (i = 1; i <= n; i++)
    if (d[i] == 1) Q.push(i);
  while (Q.size()) {
    int t = Q.front();
    Q.pop();
    if (t == u || t == v) continue;
    d[t] = -1;
    for (int x : edge[t]) {
      d[x]--;
      if (d[x] == 1) Q.push(x);
    }
    int disu = dis(u, t), disv = dis(v, t);
    if (disu < disv)
      ANS.push_back(make_pair(make_pair(v, t), t)), ans += disv;
    else
      ANS.push_back(make_pair(make_pair(u, t), t)), ans += disu;
  }
  Q.push(v);
  while (Q.size()) {
    int t = Q.front();
    Q.pop();
    for (int x : edge[t]) {
      d[x]--;
      if (d[x] == 1) Q.push(x);
    }
    ANS.push_back(make_pair(make_pair(u, t), t)), ans += dis(u, t);
  }
  printf("%I64d\n", ans);
  for (auto now : ANS)
    printf("%d %d %d\n", now.first.first, now.first.second, now.second);
}
