#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 10;
set<pair<long long, pair<long long, long long> > > s[MAXN];
vector<long long> vertex[MAXN];
vector<pair<long long, long long> > ans;
pair<pair<long long, long long>, pair<long long, long long> > f[MAXN];
long long d[MAXN];
long long in[MAXN];
bool check[MAXN];
void set_dfs(long long v) {
  check[v] = true;
  f[v] = make_pair(make_pair(0, v), make_pair(0, v));
  for (long long i = 0; i < vertex[v].size(); i++) {
    if (!check[vertex[v][i]]) {
      set_dfs(vertex[v][i]);
      pair<long long, long long> z = f[vertex[v][i]].first;
      z.first++;
      if (z > f[v].first) {
        swap(f[v].first, f[v].second);
        f[v].first = z;
      } else {
        f[v].second = max(f[v].second, z);
      }
    }
  }
}
void dfs(long long v, pair<long long, long long> t) {
  check[v] = true;
  if (t > f[v].first) {
    swap(f[v].first, f[v].second);
    f[v].first = t;
  } else {
    f[v].second = max(f[v].second, t);
  }
  pair<pair<long long, long long>, pair<long long, long long> > k =
      make_pair(t, t);
  for (long long i = 0; i < vertex[v].size(); i++) {
    if (!check[vertex[v][i]]) {
      pair<long long, long long> z = f[vertex[v][i]].first;
      z.first++;
      if (z > k.first) {
        swap(k.first, k.second);
        k.first = z;
      } else {
        k.second = max(k.second, z);
      }
    }
  }
  for (long long i = 0; i < vertex[v].size(); i++) {
    if (!check[vertex[v][i]]) {
      pair<long long, long long> z = f[vertex[v][i]].first;
      z.first++;
      if (k.first == z) {
        z = k.second;
        z.first++;
        dfs(vertex[v][i], z);
      } else {
        z = k.first;
        z.first++;
        dfs(vertex[v][i], z);
      }
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  long long n, r, res = 0;
  cin >> n;
  for (long long i = 0; i < (n - 1); i++) {
    long long v, u;
    cin >> v >> u;
    vertex[v].push_back(u);
    vertex[u].push_back(v);
  }
  set_dfs(1);
  fill(check, check + MAXN, false);
  dfs(1, make_pair(-1, 1));
  for (long long i = 1; i <= n; i++) {
    if (f[f[i].first.second].first.second == i) {
      r = max(i, f[i].first.second);
    }
  }
  f[r].first = make_pair(0, r);
  for (long long i = 1; i <= n; i++) {
    d[i] = (long long)(vertex[i].size());
    if (d[i] == 1) {
      in[f[i].first.second]++;
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (d[i] == 1) {
      s[in[i]].insert(
          make_pair(f[i].first.first, make_pair(i, f[i].first.second)));
    }
  }
  for (long long i = 0; i < (n - 1); i++) {
    pair<long long, pair<long long, long long> > k = (*s[0].begin());
    s[0].erase(s[0].begin());
    res += k.first;
    ans.push_back(make_pair(k.second.first, k.second.second));
    s[in[k.second.second]--].erase(
        make_pair(f[k.second.second].first.first,
                  make_pair(k.second.second, f[k.second.second].first.second)));
    s[in[k.second.second]].insert(
        make_pair(f[k.second.second].first.first,
                  make_pair(k.second.second, f[k.second.second].first.second)));
    for (long long j = 0; j < vertex[k.second.first].size(); j++) {
      d[vertex[k.second.first][j]]--;
      if (d[vertex[k.second.first][j]] == 1) {
        s[0].insert(make_pair(k.first - 1, make_pair(vertex[k.second.first][j],
                                                     k.second.second)));
        s[in[k.second.second]++].erase(make_pair(
            f[k.second.second].first.first,
            make_pair(k.second.second, f[k.second.second].first.second)));
        s[in[k.second.second]].insert(make_pair(
            f[k.second.second].first.first,
            make_pair(k.second.second, f[k.second.second].first.second)));
      }
    }
  }
  cout << res << endl;
  for (long long i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second << " " << ans[i].first << endl;
  }
}
