#include <bits/stdc++.h>
using namespace std;
const int maxn = 400000 + 10;
struct node {
  int x, y, z;
} ans[maxn];
int sz[maxn], root, rt, rt2, n, tot, mx, cur, now;
vector<int> g[maxn];
void dfs(int p, int fa) {
  sz[p] = 1;
  int val = 0;
  for (int i = 0; i < g[p].size(); i++) {
    int v = g[p][i];
    if (v == fa) continue;
    dfs(v, p);
    val = max(val, sz[v]);
    sz[p] += sz[v];
  }
  val = max(val, n - sz[p]);
  if (val < mx) {
    mx = val;
    rt = p;
  }
}
void dfs2(int p, int fa) {
  if (p != now) {
    ans[++tot] = (node){root, cur, p};
    ans[++tot] = (node){p, fa, now};
    cur = p;
  }
  for (int i = 0; i < g[p].size(); i++) {
    int v = g[p][i];
    if (v == fa) continue;
    dfs2(v, p);
  }
}
void work() {
  root = rt;
  for (int i = 0; i < g[rt].size(); i++) {
    int v = g[rt][i];
    if (v != rt2) {
      now = cur = v;
      dfs2(v, rt);
      ans[++tot] = (node){rt, cur, v};
    }
  }
  root = rt2;
  for (int i = 0; i < g[rt2].size(); i++) {
    int v = g[rt2][i];
    if (v != rt) {
      now = cur = v;
      dfs2(v, rt2);
      ans[++tot] = (node){rt2, cur, v};
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  mx = n + 1;
  dfs(1, 0);
  dfs(rt, 0);
  for (int i = 0; i < g[rt].size(); i++) {
    int v = g[rt][i];
    if (sz[v] >= n - sz[v]) rt2 = v;
  }
  work();
  printf("%d\n", tot);
  for (int i = 1; i <= tot; i++)
    printf("%d %d %d\n", ans[i].x, ans[i].y, ans[i].z);
}
