#include <bits/stdc++.h>
using namespace std;
const int N = -1;
const long long INF = 1.01e9;
struct Ans {
  int x, y, y1;
};
int n;
vector<vector<int>> e;
void read() {
  scanf("%d", &n);
  e.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u--;
    e[v].push_back(u);
    e[u].push_back(v);
  }
}
vector<Ans> solve() {
  vector<int> c(n);
  vector<int> cent;
  function<void(int, int)> dfs0 = [&](int v, int p) {
    c[v] = 1;
    int mx = 0;
    for (auto u : e[v]) {
      if (u == p) continue;
      dfs0(u, v);
      mx = max(mx, c[u]);
      c[v] += c[u];
    }
    mx = max(mx, n - c[v]);
    if (mx * 2 <= n) cent.push_back(v);
  };
  dfs0(0, -1);
  assert((int)cent.size() == 1 || (int)cent.size() == 2);
  vector<set<int>> g(n);
  for (int i = 0; i < n; i++) {
    g[i] = set<int>((e[i]).begin(), (e[i]).end());
  }
  vector<Ans> answer;
  auto remove = [&](int v, int u) {
    assert(g[v].erase(u));
    assert(g[u].erase(v));
  };
  auto add = [&](int v, int u) {
    assert(g[v].insert(u).second);
    assert(g[u].insert(v).second);
  };
  auto op = [&](int x, int y, int y1) {
    if (y == y1) {
      return;
    }
    answer.push_back({x, y, y1});
    remove(x, y);
    add(x, y1);
  };
  function<pair<int, int>(int, int)> dfs2 = [&](int v, int p) {
    vector<pair<int, int>> ch;
    for (auto u : e[v]) {
      if (u == p) continue;
      ch.push_back(dfs2(u, v));
    }
    if (ch.size() == 1) {
      return make_pair(v, ch[0].second);
    }
    int curV = p;
    for (int i = 0; i < (int)ch.size() - 1; i++) {
      op(curV, v, ch[i].second);
      curV = ch[i].first;
    }
    if (ch.empty()) return make_pair(v, v);
    return make_pair(ch[0].second, ch.back().second);
  };
  for (int tt = 0; tt < ((int)cent.size()); tt++) {
    int v = cent[tt];
    for (auto u : e[v]) {
      if (u == cent[(tt + 1) % (int)cent.size()]) continue;
      dfs2(u, v);
    }
  }
  for (int tt = 0; tt < ((int)cent.size()); tt++) {
    int v = cent[tt];
    vector<int> local((g[v]).begin(), (g[v]).end());
    for (auto u : local) {
      if (u == cent[(tt + 1) % (int)cent.size()]) continue;
      vector<int> tmp;
      int x = v;
      int y = u;
      tmp.push_back(v);
      for (;;) {
        tmp.push_back(y);
        assert(g[y].size() <= 2);
        if (g[y].size() == 1) break;
        int nx = -1;
        for (auto z : g[y]) {
          if (z != x) nx = z;
        }
        assert(nx != -1);
        x = y;
        y = nx;
      }
      for (int i = (int)tmp.size() - 4; i >= 0; i--) {
        op(tmp[i], tmp[i + 1], tmp[(int)tmp.size() - 2]);
      }
    }
  }
  assert((int)answer.size() <= n * 2);
  return answer;
}
void printAns(vector<Ans> answer) {
  printf("%d\n", (int)answer.size());
  for (auto A : answer) {
    printf("%d %d %d\n", A.x + 1, A.y + 1, A.y1 + 1);
  }
}
void genTest() {
  n = rand() % 200 + 1;
  if (n % 2 == 0) n++;
  e.clear();
  e.resize(n);
  vector<int> perm(n);
  for (int i = 0; i < (n); i++) perm[i] = i;
  random_shuffle((perm).begin(), (perm).end());
  cerr << "----------------" << endl;
  cerr << n << endl;
  for (int i = 1; i < n; i++) {
    int p = rand() % i;
    int v = perm[p];
    int u = perm[i];
    e[v].push_back(u);
    e[u].push_back(v);
    cerr << v + 1 << " " << u + 1 << endl;
  }
}
void check(vector<Ans> res) {
  vector<set<int>> g(n);
  auto remove = [&](int v, int u) {
    assert(g[v].erase(u));
    assert(g[u].erase(v));
  };
  auto add = [&](int v, int u) {
    assert(g[v].insert(u).second);
    assert(g[u].insert(v).second);
  };
  for (int i = 0; i < n; i++) {
    g[i] = set<int>((e[i]).begin(), (e[i]).end());
  }
  for (auto A : res) {
    remove(A.x, A.y);
    add(A.x, A.y1);
  }
  vector<int> dist;
  auto bfs = [&](int st) {
    dist.assign(n, INF);
    dist[st] = 0;
    queue<int> q;
    q.push(st);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto u : g[v]) {
        if (dist[u] > dist[v] + 1) {
          dist[u] = dist[v] + 1;
          q.push(u);
        }
      }
    }
    int mx = 0;
    for (int i = 0; i < n; i++) {
      if (dist[i] > dist[mx]) {
        mx = i;
      }
    }
    return mx;
  };
  int top = bfs(0);
  cerr << "dist"
       << ": ";
  for (auto& xxxx : dist) cerr << xxxx << " ";
  cerr << endl;
  ;
  int t2 = bfs(top);
  cerr << "("
       << "top"
       << ", "
       << "t2"
       << ") = (" << top << ", " << t2 << ")\n";
  ;
  cerr << "dist[t2]"
       << " = " << dist[t2] << endl;
  assert(dist[t2] <= 4);
}
int main() {
  if (1) {
    read();
    auto res = solve();
    printAns(res);
  } else {
    for (int it = 0;; it++) {
      cerr << "it"
           << " = " << it << endl;
      genTest();
      auto res = solve();
      check(res);
    }
  }
  return 0;
}
