#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
std::vector<std::vector<int>> nb;
std::vector<std::vector<int>> ch;
int n;
std::vector<int> st;
std::vector<int> par;
int root = -1;
int root2 = -1;
void dfs(int v, int p) {
  st[v] = 1;
  for (auto u : nb[v]) {
    if (u == p) continue;
    dfs(u, v);
    par[u] = v;
    st[v] += st[u];
    ch[v].push_back(u);
  }
  if (root == -1 && 2 * st[v] >= n) root = v;
}
vector<deque<int>> her;
std::vector<int> ax, ay, ay2;
void straighten(int v, int p) {
  for (auto u : ch[v]) straighten(u, v);
  if (ch[v].empty())
    her[v] = deque<int>(1, v);
  else {
    int mind = -1;
    int best = 0;
    for (auto u : ch[v])
      if (her[u].size() > best) {
        best = her[u].size();
        mind = u;
      }
    int mu = mind;
    int curp = p;
    int curtop = v;
    std::vector<int> indi;
    for (auto u : ch[v]) {
      if (u == mu) continue;
      ax.push_back(curp);
      ay.push_back(curtop);
      ay2.push_back(her[u].back());
      curp = her[u].front();
      indi.push_back(u);
    }
    her[v] = std::move(her[mu]);
    her[mu].clear();
    her[v].push_front(v);
    reverse((indi).begin(), (indi).end());
    for (auto ind : indi) {
      for (auto x : her[ind]) her[v].push_front(x);
    }
  }
}
int main() {
  scanf("%d", &n);
  nb.resize(n);
  st.resize(n);
  par = std::vector<int>(n, -1);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    nb[u].push_back(v);
    nb[v].push_back(u);
  }
  ch.clear();
  ch.resize(n);
  her.resize(n);
  dfs(0, -1);
  ch.clear();
  ch.resize(n);
  dfs(root, -1);
  for (int i = 0; i < n; i++) {
    if (i != root && 2 * st[i] >= n) root2 = i;
  }
  for (auto x : ch[root])
    if (x != root2) straighten(x, root);
  if (root2 != -1)
    for (auto x : ch[root2])
      if (x != root) straighten(x, root2);
  for (auto x : ch[root])
    if (x != root2) {
      if (her[x].size() > 2) {
        her[x].push_front(root);
        int k = her[x].size();
        for (int i = k - 4; i >= 0; i--) {
          ax.push_back(her[x][i]);
          ay.push_back(her[x][i + 1]);
          ay2.push_back(her[x][k - 2]);
        }
      }
    }
  if (root2 != -1)
    for (auto x : ch[root2])
      if (x != root) {
        if (her[x].size() > 2) {
          her[x].push_front(root2);
          int k = her[x].size();
          for (int i = k - 4; i >= 0; i--) {
            ax.push_back(her[x][i]);
            ay.push_back(her[x][i + 1]);
            ay2.push_back(her[x][k - 2]);
          }
        }
      }
  printf("%d\n", ax.size());
  for (int i = 0; i < ax.size(); i++)
    printf("%d %d %d\n", ax[i] + 1, ay[i] + 1, ay2[i] + 1);
}
