#include <bits/stdc++.h>
using namespace std;
int n, pointleft, pointright, maxdis, tt, flag;
int fa[(200000 + 5)], vis[(200000 + 5)], depth[(200000 + 5)], tag[(200000 + 5)],
    dis[(200000 + 5)], road[(200000 + 5)];
int disl[(200000 + 5)], disr[(200000 + 5)], son[(200000 + 5)];
int a[(200000 + 5)], b[(200000 + 5)], c[(200000 + 5)], rt;
long long ans;
vector<int> edge[(200000 + 5)];
queue<int> leaf;
void bfs(int u) {
  queue<int> q;
  q.push(u);
  vis[u] = 1;
  while (!q.empty()) {
    int p = q.front();
    vis[p] = 1;
    q.pop();
    for (int i = 0; i < edge[p].size(); i++) {
      int now = edge[p][i];
      if (!vis[now]) {
        dis[now] = dis[p] + 1;
        q.push(now);
      }
    }
  }
}
void dfs(int u) {
  if (u == pointright) {
    flag = 1;
    tag[u] = 1;
    road[++tt] = u;
    return;
  }
  for (int i = 0; i < edge[u].size(); i++) {
    int now = edge[u][i];
    if (!vis[now]) {
      vis[now] = 1;
      dfs(now);
    }
    if (flag == 1) {
      tag[u] = 1;
      road[++tt] = u;
      return;
    }
  }
}
void dfs2(int u, int fat) {
  if (edge[u].size() == 1 && !tag[u]) {
    leaf.push(u);
    return;
  }
  for (int i = 0; i < edge[u].size(); i++) {
    int now = edge[u][i];
    if (now != fat) {
      fa[now] = u;
      dis[now] = dis[u] + 1;
      if (!tag[now]) son[u]++;
      dfs2(now, u);
    }
  }
}
void clearing() {
  memset(vis, 0, sizeof(vis));
  memset(dis, 0, sizeof(dis));
}
void dijkstral(int Begin) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(make_pair(0, Begin));
  disl[Begin] = 0;
  vis[Begin] = 1;
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (int i = 0; i < edge[u].size(); i++) {
      int now = edge[u][i];
      if (disl[now] > disl[u] + 1) {
        disl[now] = disl[u] + 1;
        if (!vis[now]) {
          q.push(make_pair(disl[now], now));
        }
      }
    }
  }
}
void dijkstrar(int Begin) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(make_pair(0, Begin));
  disr[Begin] = 0;
  vis[Begin] = 1;
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (int i = 0; i < edge[u].size(); i++) {
      int now = edge[u][i];
      if (disr[now] > disr[u] + 1) {
        disr[now] = disr[u] + 1;
        if (!vis[now]) {
          q.push(make_pair(disr[now], now));
        }
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  memset(disl, 0x3f, sizeof(disl));
  memset(disr, 0x3f, sizeof(disr));
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  bfs(1);
  for (int i = 1; i <= n; i++)
    if (dis[i] > maxdis) {
      maxdis = dis[i];
      pointleft = i;
    }
  clearing();
  maxdis = 0;
  bfs(pointleft);
  for (int i = 1; i <= n; i++)
    if (dis[i] > maxdis) {
      maxdis = dis[i];
      pointright = i;
    }
  clearing();
  vis[pointleft] = 1;
  dfs(pointleft);
  clearing();
  for (int i = 1; i <= n; i++) {
    if (tag[i]) {
      rt = i;
      break;
    }
  }
  dfs2(rt, 0);
  clearing();
  dijkstral(pointleft);
  clearing();
  dijkstrar(pointright);
  clearing();
  int cnt = 0;
  while (!leaf.empty()) {
    int u = leaf.front();
    leaf.pop();
    son[fa[u]]--;
    b[++cnt] = u;
    c[cnt] = u;
    if (disl[u] > disr[u]) {
      a[cnt] = pointleft;
      ans += disl[u];
    } else {
      a[cnt] = pointright;
      ans += disr[u];
    }
    if (!tag[fa[u]] && !son[fa[u]]) {
      leaf.push(fa[u]);
    }
  }
  for (int i = 1; i < tt; i++) {
    c[++cnt] = road[i];
    b[cnt] = road[i];
    a[cnt] = pointleft;
    ans += tt - i;
  }
  printf("%lld\n", ans);
  for (int i = 1; i <= cnt; i++) {
    printf("%d %d %d\n", a[i], b[i], c[i]);
  }
  return 0;
}
