#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> G[200005];
int Next[200005];
int Array[200005];
int Sz[200005], Size[200005];
int sum, cnt;
bool Blocked[200005];
struct Solution {
  int x, y, y2;
} S[400005];
bool first;
int End[200005], Start[200005];
int aux, source;
unordered_set<int> X[200005];
void Read() {
  scanf("%d", &N);
  for (int i = 2; i <= N; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
    X[x].insert(y);
    X[y].insert(x);
  }
}
void DFS(int node, int father) {
  Sz[node] = 1;
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (neighb == father || Blocked[neighb] == 1) continue;
    DFS(neighb, node);
    Sz[node] += Sz[neighb];
  }
}
int findCentroid(int node, int father, int size) {
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (neighb == father || Blocked[neighb] == 1) continue;
    if (Sz[neighb] > size / 2) return findCentroid(neighb, node, size);
  }
  return node;
}
void makeBamboo(int node, int father) {
  int f = father;
  int ct = 0;
  int last = node, p;
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (neighb == father) continue;
    makeBamboo(neighb, node);
    p = neighb;
    if (ct == 0) Start[node] = End[neighb];
    last = End[neighb];
    ct++;
  }
  if (ct == 0)
    End[node] = Start[node] = node;
  else {
    End[node] = last;
    if (ct == 1) {
      Next[father] = node;
      Next[node] = Start[p];
      Start[node] = node;
      return;
    }
    Next[node] = Start[p];
    for (int i = 0; i < G[node].size(); i++) {
      int neighb = G[node][i];
      if (neighb == father || End[neighb] == End[node]) continue;
      ++cnt;
      S[cnt].x = f;
      S[cnt].y = node;
      S[cnt].y2 = End[neighb];
      if (f == source) aux = End[neighb];
      X[f].erase(node);
      X[node].erase(f);
      X[f].insert(End[neighb]);
      X[End[neighb]].insert(f);
      Next[f] = End[neighb];
      f = Start[neighb];
      Next[f] = node;
    }
  }
}
void makeStar(int node) {
  int ct = 0;
  int ind = 0;
  Array[++ind] = source;
  Array[++ind] = aux;
  node = aux;
  bool ok = 1;
  while (X[node].size() == 2) {
    ok = 0;
    auto it = X[node].begin();
    int n = *it;
    if (Array[ind - 1] == n) {
      it = next(it);
      n = *it;
    }
    Array[++ind] = n;
    node = n;
  }
  for (int i = ind - 2; i >= 1; i--) {
    ++cnt;
    S[cnt].x = Array[i];
    S[cnt].y = Array[i + 1];
    S[cnt].y2 = Array[ind];
  }
}
int main() {
  Read();
  int root = 1;
  DFS(root, 0);
  bool ok = 0;
  int center = findCentroid(root, 0, Sz[root]);
  for (int i = 0; i < G[center].size(); i++) {
    int neighb = G[center][i];
    if (Sz[neighb] == N - Sz[neighb]) {
      ok = 1;
      Blocked[center] = 1;
      Blocked[neighb] = 1;
      int node = center;
      for (int i = 0; i < G[node].size(); i++) {
        int neighb = G[node][i];
        int from = center;
        aux = neighb;
        source = node;
        if (Blocked[neighb] == 1) continue;
        makeBamboo(neighb, node);
        makeStar(node);
      }
      node = neighb;
      for (int i = 0; i < G[node].size(); i++) {
        int neighb = G[node][i];
        int from = center;
        aux = neighb;
        source = node;
        if (Blocked[neighb] == 1) continue;
        makeBamboo(neighb, node);
        makeStar(node);
      }
      break;
    }
  }
  if (ok == 0) {
    int node = center;
    for (int i = 0; i < G[node].size(); i++) {
      int neighb = G[node][i];
      int from = center;
      aux = neighb;
      source = node;
      if (Blocked[neighb] == 1) continue;
      makeBamboo(neighb, node);
      makeStar(node);
    }
  }
  printf("%d\n", cnt);
  for (int i = 1; i <= cnt; i++) {
    printf("%d %d %d\n", S[i].x, S[i].y, S[i].y2);
  }
  return 0;
}
