#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long INF = 1LL << 60;
const int MaxN = 200000;
int n, diaL, diaR, all;
int pre[2 * MaxN + 5], last[MaxN + 5], other[2 * MaxN + 5];
int dep[MaxN + 5], dis[MaxN + 5], ind[MaxN + 5];
int up[MaxN + 5][22];
bool is_dia[MaxN + 5];
long long res;
vector<int> diameter;
struct Ask {
  int u, v, del;
  Ask() {}
  Ask(int _u, int _v, int _del) {
    u = _u;
    v = _v;
    del = _del;
  }
};
vector<Ask> ans;
void build(int x, int y) {
  pre[++all] = last[x];
  last[x] = all;
  other[all] = y;
}
void Dfs(int x, int fa) {
  dep[x] = dep[fa] + 1;
  up[x][0] = fa;
  for (int i = 1; i <= 20; i++) up[x][i] = up[up[x][i - 1]][i - 1];
  for (int ed = last[x]; ed != -1; ed = pre[ed]) {
    int dr = other[ed];
    if (dr != fa) {
      Dfs(dr, x);
      ind[x]++;
    }
  }
}
int Get_LCA(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int d = dep[x] - dep[y];
  for (int i = 0; i <= 20; i++)
    if (d & (1 << i)) x = up[x][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; i--) {
    if (up[x][i] != up[y][i]) {
      x = up[x][i];
      y = up[y][i];
    }
  }
  return up[x][0];
}
int get_dist(int x, int y) {
  int u = Get_LCA(x, y);
  return dep[x] + dep[y] - 2 * dep[u];
}
void dia_Dfs(int x, int fa) {
  dis[x] = dis[fa] + 1;
  for (int ed = last[x]; ed != -1; ed = pre[ed]) {
    int dr = other[ed];
    if (dr != fa) dia_Dfs(dr, x);
  }
}
void Get_diameter() {
  dia_Dfs(1, 0);
  int MAX = 0;
  for (int i = 1; i <= n; i++) {
    if (dis[i] > MAX) {
      MAX = dis[i];
      diaL = i;
    }
  }
  memset(dis, 0, sizeof(dis));
  dia_Dfs(diaL, 0);
  MAX = 0;
  for (int i = 1; i <= n; i++) {
    if (dis[i] > MAX) {
      MAX = dis[i];
      diaR = i;
    }
  }
}
void mark_diameter() {
  int now = diaR;
  while (now != diaL) {
    diameter.push_back(now);
    is_dia[now] = true;
    now = up[now][0];
  }
  diameter.push_back(diaL);
  is_dia[diaL] = true;
}
void del_not_diameter() {
  queue<int> Q;
  for (int i = 1; i <= n; i++)
    if (ind[i] == 0 && !is_dia[i]) Q.push(i);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    int lx = get_dist(cur, diaL);
    int ly = get_dist(cur, diaR);
    res += 1LL * max(lx, ly);
    if (lx > ly)
      ans.push_back(Ask(cur, diaL, cur));
    else
      ans.push_back(Ask(cur, diaR, cur));
    int father = up[cur][0];
    ind[father]--;
    if (!is_dia[father] && ind[father] == 0) Q.push(father);
  }
}
void del_diameter() {
  int tmp = (int)diameter.size();
  res += 1LL * tmp * (tmp - 1) / 2LL;
  for (int i = 0; i < diameter.size() - 1; i++)
    ans.push_back(Ask(diameter[i], diaL, diameter[i]));
}
void solve() {
  res = 0LL;
  ans.clear();
  diameter.clear();
  memset(is_dia, 0, sizeof(is_dia));
  mark_diameter();
  del_not_diameter();
  del_diameter();
  cout << res << endl;
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d %d\n", ans[i].u, ans[i].v, ans[i].del);
}
int main() {
  while (scanf("%d", &n) != EOF) {
    all = -1;
    memset(last, -1, sizeof(last));
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d %d", &u, &v);
      build(u, v);
      build(v, u);
    }
    Get_diameter();
    Dfs(diaL, 0);
    solve();
  }
  return 0;
}
