#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = acos(-1);
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T abs(T x) {
  return x < 0 ? -x : x;
}
void DFSD(const vector<vector<int>>& adj, vector<int>& num, vector<int>& p,
          int v, int cur_p) {
  num[v] = 1;
  p[v] = cur_p;
  for (int i = 0; i < ((int)(adj[v]).size()); ++i) {
    int u = adj[v][i];
    if (u != cur_p) {
      DFSD(adj, num, p, u, v);
      num[v] += num[u];
    }
  }
}
int GetCntr(const vector<vector<int>>& adj, int n) {
  vector<int> num(n);
  vector<int> p(n);
  DFSD(adj, num, p, 0, -1);
  int half = n / 2;
  for (int v = 0; v < n; ++v) {
    bool key = true;
    int ost = n - 1;
    for (int i = 0; i < ((int)(adj[v]).size()); ++i) {
      int u = adj[v][i];
      if (u != p[v]) {
        key &= (num[u] <= half);
        ost -= num[u];
      }
    }
    key &= (ost <= half);
    if (key) {
      return v;
    }
  }
  return -1;
}
const int MAXN = 250 * 1000;
int buf[MAXN];
void Do1(vector<vector<int>>& adj, vector<pair<int, pair<int, int>>>& res,
         vector<int>& fin, int v, int p) {
  for (int ti = 0; ti < ((int)(adj[v]).size()); ++ti) {
    int u = adj[v][ti];
    if (u == p) {
      continue;
    }
    Do1(adj, res, fin, u, v);
    int num = 0;
    for (int j = 0; j < ((int)(adj[u]).size()); ++j) {
      if (adj[u][j] != v) {
        buf[num] = adj[u][j];
        ++num;
      }
    }
    if (num == 0) {
      fin[u] = u;
      continue;
    }
    res.push_back(make_pair(v, make_pair(u, fin[buf[num - 1]])));
    for (int j = num - 1; j >= 1; --j) {
      res.push_back(make_pair(buf[j], make_pair(u, fin[buf[j - 1]])));
    }
    adj[v][ti] = fin[buf[num - 1]];
    adj[fin[buf[num - 1]]].push_back(v);
    for (int j = num - 1; j >= 1; --j) {
      if (adj[buf[j]][0] == u) {
        adj[buf[j]][0] = fin[buf[j - 1]];
      } else {
        adj[buf[j]][1] = fin[buf[j - 1]];
      }
      adj[fin[buf[j - 1]]].push_back(buf[j]);
    }
    adj[u].clear();
    adj[u].push_back(buf[0]);
    fin[fin[buf[num - 1]]] = u;
  }
}
void Do2(vector<vector<int>>& adj, vector<pair<int, pair<int, int>>>& res,
         int v, int p) {
  int cur_v = v;
  int cur_p = p;
  int num = 1;
  buf[0] = cur_p;
  while (((int)(adj[cur_v]).size()) != 1) {
    buf[num] = cur_v;
    ++num;
    int new_v = -1;
    if (adj[cur_v][0] == cur_p) {
      new_v = adj[cur_v][1];
    } else {
      new_v = adj[cur_v][0];
    }
    cur_p = cur_v;
    cur_v = new_v;
  }
  for (int i = num - 2; i >= 0; --i) {
    res.push_back(make_pair(buf[i], make_pair(buf[i + 1], cur_v)));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  for (int i = 0; i < n - 1; ++i) {
    int v1, v2;
    cin >> v1 >> v2;
    --v1, --v2;
    adj[v1].push_back(v2);
    adj[v2].push_back(v1);
  }
  int cntr = GetCntr(adj, n);
  vector<int> num(n);
  vector<int> p(n);
  DFSD(adj, num, p, cntr, -1);
  int cntr_plus = -1;
  for (int i = 0; i < ((int)(adj[cntr]).size()); ++i) {
    int u = adj[cntr][i];
    if (num[u] >= n - num[u]) {
      cntr_plus = u;
    }
  }
  vector<pair<int, pair<int, int>>> res;
  vector<int> fin(n);
  Do1(adj, res, fin, cntr, cntr_plus);
  for (int i = 0; i < ((int)(adj[cntr]).size()); ++i) {
    if (adj[cntr][i] != cntr_plus) {
      Do2(adj, res, adj[cntr][i], cntr);
    }
  }
  if (cntr_plus != -1) {
    swap(cntr, cntr_plus);
    Do1(adj, res, fin, cntr, cntr_plus);
    for (int i = 0; i < ((int)(adj[cntr]).size()); ++i) {
      if (adj[cntr][i] != cntr_plus) {
        Do2(adj, res, adj[cntr][i], cntr);
      }
    }
  }
  cout << ((int)(res).size()) << "\n";
  for (int i = 0; i < ((int)(res).size()); ++i) {
    cout << res[i].first + 1 << " " << res[i].second.first + 1 << " "
         << res[i].second.second + 1 << "\n";
  }
  return 0;
}
