#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, x, y, z, m, Q, mx, used[200500], w[200500], W[200500], start;
vector<int> g[200500], verts, leafs, ned;
set<int> f[200500];
vector<pair<pair<int, int>, int> > ans;
void dfs(int v, int p = -1) {
  w[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (to != p) {
      dfs(to, v);
      w[v] += w[to];
    }
  }
  if (w[v] == n / 2 && n % 2 == 0) {
    x = v, y = p;
  }
  if (w[v] > mx && w[v] <= n / 2) {
    mx = w[v];
    z = p;
  }
  if (n - w[v] > mx && n - w[v] <= n / 2) {
    mx = n - w[v];
    z = v;
  }
}
void dfs_prepare(int v, int p) {
  verts.push_back(v);
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (to != p) {
      f[v].insert(to);
      f[to].insert(v);
      dfs_prepare(to, v);
    }
  }
}
void dfs_mag(int v, int p = -1) {
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (to != p) {
      f[v].insert(to);
      f[to].insert(v);
      verts.push_back(v);
      dfs_prepare(to, v);
      for (int j = 0; j < verts.size(); j++) {
        if (f[verts[j]].size() == 1) leafs.push_back(verts[j]);
      }
      int cur = v, prev = -1;
      while (leafs.size() != 2) {
        for (auto itr = f[cur].begin(); itr != f[cur].end(); itr++) {
          int To = *itr;
          if (To != prev) {
            if (f[To].size() > 2) {
              int t = leafs.back();
              leafs.pop_back();
              f[cur].erase(To);
              f[To].erase(cur);
              f[cur].insert(t);
              f[t].insert(cur);
              ans.push_back(make_pair(make_pair(cur, To), t));
              prev = cur;
              cur = t;
              break;
            } else {
              prev = cur;
              cur = To;
              break;
            }
          }
        }
      }
      cur = v;
      ned.push_back(v);
      cur = (*(f[cur].begin()));
      ned.push_back(cur);
      prev = v;
      while (f[cur].size() != 1) {
        for (auto itr = f[cur].begin(); itr != f[cur].end(); itr++) {
          if ((*itr) != prev) {
            ned.push_back(*itr);
            prev = cur;
            cur = *itr;
            break;
          }
        }
      }
      int siz = ned.size();
      for (int j = siz - 3; j >= 0; j--) {
        ans.push_back(make_pair(make_pair(ned[j], ned[j + 1]), ned.back()));
      }
      for (int j = 0; j < verts.size(); j++) {
        f[verts[j]].clear();
      }
      ned.clear();
      leafs.clear();
      verts.clear();
    }
  }
}
int main() {
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    scanf("%d %d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  mx = 0;
  x = -1, y = -1;
  dfs(1);
  if (x != -1) {
    dfs_mag(y, x);
    dfs_mag(x, y);
  } else
    dfs_mag(z, -1);
  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    printf("%d %d %d\n", ans[i].first.first, ans[i].first.second,
           ans[i].second);
  }
  return 0;
}
