#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long linf = (long long)1e18;
const int mod = (int)1e9 + 7;
const long double eps = (long double)1e-8;
const int maxn = (int)2e5 + 5;
const long double pi = acos(-1);
vector<pair<pair<int, int>, int>> ans;
int centre, centre2;
vector<int> a[maxn];
set<pair<int, int>> q;
int sz[maxn], en[maxn], rv[maxn];
int n;
void dfs0(int v, int p = -1) {
  int fl = 1;
  sz[v] = 1;
  for (auto to : a[v]) {
    if (to == p) continue;
    dfs0(to, v);
    sz[v] += sz[to];
    if (sz[to] >= n / 2 + n % 2) fl = 0;
  }
  if (n - sz[v] <= n / 2 && fl) {
    centre = v;
    centre2 = p;
  }
}
void answer() {
  assert(((int)(ans).size()) <= 2 * n);
  cout << ((int)(ans).size()) << endl;
  for (auto p : ans) {
    cout << p.first.first << " " << p.first.second << " " << p.second << "\n";
  }
  exit(0);
}
void upd(int x, int y, int ty) {
  ans.push_back({{x, y}, ty});
  q.erase({min(x, y), max(x, y)});
  q.insert({min(x, ty), max(x, ty)});
}
void solve(int v, int p) {
  int cp = p;
  en[v] = v;
  rv[v] = v;
  for (auto i = 0; i < ((int)(a[v]).size()); ++i) {
    int to = a[v][i];
    if (to == p) continue;
    solve(to, v);
    if (i == ((int)(a[v]).size()) - 1 ||
        (i == ((int)(a[v]).size()) - 2 &&
         a[v][((int)(a[v]).size()) - 1] == p)) {
      en[v] = en[to];
      break;
    }
    int ten = en[to];
    upd(cp, v, ten);
    if (cp == p) {
      rv[v] = ten;
    }
    cp = rv[to];
  }
  en[rv[v]] = en[v];
}
void sol_centre(int centre, int centre2) {
  for (auto to : a[centre]) {
    if (to != centre2) solve(to, centre);
  }
}
void dfs(int v, int p, int z) {
  for (auto to : a[v]) {
    if (to == p) continue;
    dfs(to, v, z);
    if (to != z) upd(v, to, z);
  }
}
void sol2_centre(int centre, int centre2) {
  for (auto to : a[centre]) {
    if (to != centre2) {
      dfs(to, centre, en[to]);
      if (to != en[to]) upd(centre, to, en[to]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  q.clear();
  for (auto i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
    q.insert({min(x, y), max(x, y)});
  }
  centre = -1;
  dfs0(1);
  assert(centre != -1);
  if (sz[centre] == n / 2 && n % 2 == 0) {
  } else {
    centre2 = -1;
  }
  ans.clear();
  sol_centre(centre, centre2);
  if (centre2 != -1) {
    sol_centre(centre2, centre);
  }
  for (auto i = 1; i <= n; ++i) {
    a[i].clear();
  }
  for (auto e : q) {
    a[e.first].push_back(e.second);
    a[e.second].push_back(e.first);
  }
  sol2_centre(centre, centre2);
  if (centre2 != -1) {
    sol2_centre(centre2, centre);
  }
  answer();
  return 0;
}
