#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<stack>
#include<unordered_map>
#include<bitset>
#include<limits>
#include<complex>
#include<array>
#include<numeric>
#include<functional>
#include<random>

using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++)
#define REP(i,n) rep(i,0,n)
#define all(hoge) (hoge).begin(),(hoge).end()
typedef pair<ll, ll> P;
constexpr long double m_pi = 3.1415926535897932L;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1LL << 61;
constexpr long double EPS = 1e-10;
template<typename T> using vector2 = vector<vector<T>>;
template<typename T> using vector3 = vector<vector2<T>>;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
string operator*(const string& s, int k) {
	if (k == 0) return "";
	string p = (s + s) * (k / 2);
	if (k % 2 == 1) p += s;
	return p;
}

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; }

struct Edge {//グラフ
	int to, rev; ll cap;
	Edge(int _to, ll _cap, int _rev) {
		to = _to; cap = _cap; rev = _rev;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph& G, int from, int to, ll cap, bool revFlag, ll revCap) {//最大フロー求める Ford-fulkerson
	G[from].push_back(Edge(to, cap, (ll)G[to].size() + (from == to)));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));//最小カットの場合逆辺は0にする
}

ll max_flow_dfs(Graph& G, ll v, ll t, ll f, vector<bool>& used)
{
	if (v == t)
		return f;
	used[v] = true;
	for (int i = 0; i < G[v].size(); ++i) {
		Edge& e = G[v][i];
		if (!used[e.to] && e.cap > 0) {
			ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used);
			if (d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}
//二分グラフの最大マッチングを求めたりも出来る　また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ
ll max_flow(Graph& G, ll s, ll t)//O(V(V+E))
{
	ll flow = 0;
	for (;;) {
		vector<bool> used(G.size());
		REP(i, used.size())used[i] = false;
		ll f = max_flow_dfs(G, s, t, INF, used);
		if (f == 0) {
			return flow;
		}
		flow += f;
	}
}

void BellmanFord(Graph& G, ll s, Array& d, Array& negative) {//O(|E||V|)
	d.resize(G.size());
	negative.resize(G.size());
	REP(i, d.size())d[i] = INF;
	REP(i, d.size())negative[i] = false;
	d[s] = 0;
	REP(k, G.size() - 1) {
		REP(i, G.size()) {
			REP(j, G[i].size()) {
				if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) {
					d[G[i][j].to] = d[i] + G[i][j].cap;
				}
			}
		}
	}
	REP(k, G.size() - 1) {
		REP(i, G.size()) {
			REP(j, G[i].size()) {
				if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) {
					d[G[i][j].to] = d[i] + G[i][j].cap;
					negative[G[i][j].to] = true;
				}
				if (negative[i] == true)negative[G[i][j].to] = true;
			}
		}
	}
}
void Dijkstra(Graph& G, ll s, Array& d) {//O(|E|log|V|)
	d.resize(G.size());
	REP(i, d.size())d[i] = INF;
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P>> q;
	q.push(make_pair(0, s));
	while (!q.empty()) {
		P a = q.top();
		q.pop();
		if (d[a.second] < a.first)continue;
		REP(i, G[a.second].size()) {
			Edge e = G[a.second][i];
			if (d[e.to] > d[a.second] + e.cap) {
				d[e.to] = d[a.second] + e.cap;
				q.push(make_pair(d[e.to], e.to));
			}
		}
	}
}
void WarshallFloyd(Graph& G, Matrix& d) {//O(V^3)
	d.resize(G.size());
	REP(i, d.size())d[i].resize(G.size());
	REP(i, d.size()) {
		REP(j, d[i].size()) {
			d[i][j] = ((i != j) ? INF : 0);
		}
	}
	REP(i, G.size()) {
		REP(j, G[i].size()) {
			chmin(d[i][G[i][j].to], G[i][j].cap);
		}
	}
	REP(i, G.size()) {
		REP(j, G.size()) {
			REP(k, G.size()) {
				if (d[j][i] != INF && d[i][k] != INF)chmin(d[j][k], d[j][i] + d[i][k]);
			}
		}
	}
}
bool tsort(Graph& graph, vector<int>& order) {//トポロジカルソートO(E+V)
	int n = graph.size(), k = 0;
	vector<int> in(n);
	for (auto& es : graph)
		for (auto& e : es)in[e.to]++;
	priority_queue<int, vector<int>, greater<int>> que;
	REP(i, n)
		if (in[i] == 0)que.push(i);
	while (que.size()) {
		int v = que.top();
		que.pop();
		order.push_back(v);
		for (auto& e : graph[v])
			if (--in[e.to] == 0)que.push(e.to);
	}
	if (order.size() != n)return false;
	else return true;
}
class Lca {
public:
	const int n = 0;
	const int log2_n = 0;
	std::vector<std::vector<int>> parent;
	std::vector<int> depth;

	Lca() {}

	Lca(const Graph& g, int root)
		: n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) {
		dfs(g, root, -1, 0);
		for (int k = 0; k + 1 < log2_n; k++) {
			for (int v = 0; v < (int)g.size(); v++) {
				if (parent[k][v] < 0)
					parent[k + 1][v] = -1;
				else
					parent[k + 1][v] = parent[k][parent[k][v]];
			}
		}
	}

	void dfs(const Graph& g, int v, int p, int d) {
		parent[0][v] = p;
		depth[v] = d;
		for (auto& e : g[v]) {
			if (e.to != p) dfs(g, e.to, v, d + 1);
		}
	}

	int get(int u, int v) {
		if (depth[u] > depth[v]) std::swap(u, v);
		for (int k = 0; k < log2_n; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = log2_n - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
};

class UnionFind {
	vector<int> data;
	int n;
public:
	UnionFind(int size) : data(size, -1), n(size) { }
	bool merge(int x, int y) {//xとyの集合を統合する
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		n -= (x != y);
		return x != y;
	}
	bool same(int x, int y) {//xとyが同じ集合か返す
		return root(x) == root(y);
	}
	int root(int x) {//xのルートを返す
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {//xの集合のサイズを返す
		return -data[root(x)];
	}
	int num() {//集合の数を返す
		return n;
	}
};

template<typename T, typename F>
class SegmentTree {
private:
	T identity;
	F merge;
	ll n;
	vector<T> dat;
public:
	SegmentTree(F f, T id, vector<T> v) :merge(f), identity(id) {
		int _n = v.size();
		n = 1;
		while (n < _n)n *= 2;
		dat.resize(2 * n - 1, identity);
		REP(i, _n)dat[n + i - 1] = v[i];
		for (int i = n - 2; i >= 0; i--)dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	SegmentTree(F f, T id, int _n) :merge(f), identity(id) {
		n = 1;
		while (n < _n)n *= 2;
		dat.resize(2 * n - 1, identity);
	}
	void set_val(int i, T x) {
		i += n - 1;
		dat[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
	}
	T query(int l, int r) {
		T left = identity, right = identity;
		l += n - 1; r += n - 1;
		while (l < r) {
			if ((l & 1) == 0)left = merge(left, dat[l]);
			if ((r & 1) == 0)right = merge(dat[r - 1], right);
			l = l / 2;
			r = (r - 1) / 2;
		}
		return merge(left, right);
	}
};

template< typename T >
class FenwickTree {
	vector< T > data;
	int n;
	int p;
public:
	FenwickTree(int n) :n(n) {
		data.resize(n + 1LL, 0);
		p = 1;
		while (p < data.size())p *= 2;
	}
	T sum(int k) {
		T ret = 0;
		for (; k > 0; k -= k & -k) ret += data[k];
		return (ret);
	}

	T sum(int a, int b) { return sum(b) - sum(a); }//[a,b)

	void add(int k, T x) {
		for (++k; k <= n; k += k & -k) data[k] += x;
	}

	int lower_bound(ll w) {
		if (w <= 0)return -1;
		int x = 0;
		for (int k = p / 2; k > 0; k /= 2) {
			if (x + k <= n && data[x + k] < w)w -= data[x + k], x += k;
		}
		return x;
	}
};

//約数求める //約数
void divisor(ll n, vector<ll>& ret) {
	for (ll i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			ret.push_back(i);
			if (i * i != n) ret.push_back(n / i);
		}
	}
	sort(ret.begin(), ret.end());
}

void prime_factorization(ll n, vector<P>& ret) {
	for (ll i = 2; i * i <= n; i++) {
		if (n % i == 0) {
			ret.push_back({ i,0 });
			while (n % i == 0) {
				n /= i;
				ret[ret.size() - 1].second++;
			}
		}
	}
	if (n != 1)ret.push_back({ n,1 });
}

inline ll mod_pow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % mod;
		x = x * x % mod;
		n >>= 1;
	}
	return res;
}

inline ll mod_inv(ll x, ll mod) {
	return mod_pow(x, mod - 2, mod);
}

class Combination {
public:
	Array fact;
	Array fact_inv;
	ll mod;
	//if n >= mod use lucas
	ll nCr(ll n, ll r) {
		if (n < r)return 0;
		if (n < mod)return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod;

		ll ret = 1;
		while (n || r) {
			ll _n = n % mod, _r = r % mod;
			n /= mod; r /= mod;
			(ret *= nCr(_n, _r)) %= mod;
		}
		return ret;
	}
	ll nPr(ll n, ll r) {
		return (fact[n] * fact_inv[n - r]) % mod;
	}
	ll nHr(ll n, ll r) {
		return nCr(r + n - 1, r);
	}
	Combination(ll _n, ll _mod) {
		mod = _mod;
		ll n = min(_n + 1, mod);
		fact.resize(n);
		fact[0] = 1;
		REP(i, n - 1) {
			fact[i + 1] = (fact[i] * (i + 1LL)) % mod;
		}
		fact_inv.resize(n);
		fact_inv[n - 1] = mod_inv(fact[n - 1], mod);
		for (int i = n - 1; i > 0; i--) {
			fact_inv[i - 1] = fact_inv[i] * i % mod;
		}
	}
};

ll popcount(ll x) {
	x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
	x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
	x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F);
	x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF);
	x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF);
	x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF);

	return x;
}

int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);

	ll n;
	cin >> n;
	Graph g(n);
	REP(i, n - 1) {
		ll u, v;
		cin >> u >> v;
		u--; v--;
		add_edge(g, u, v, 1, true, 1);
	}

	Array d1(n,0), d2(n,0);
	auto dfs = [&](auto dfs, ll v, ll par, Array& d)->P {
		P ret = { d[v],v };
		for (auto e : g[v]) {
			if (e.to == par)continue;
			d[e.to] = d[v] + 1;
			chmax(ret, dfs(dfs, e.to, v, d));
		}
		return ret;
	};
	ll s = dfs(dfs, 0, -1, d1).second;
	d1[s] = 0;
	ll t = dfs(dfs, s, -1, d1).second;
	d2[t] = 0;
	ll d = dfs(dfs, t, -1, d2).first;
	ll ans = 0;
	using T = tuple<ll, ll, ll>;
	set<T> a, b;
	s++; t++;
	REP(i, n) {
		if (d1[i] + d2[i] == d) {
			if (d1[i] != 0)b.insert({ d1[i],i + 1,s }), ans += d1[i];
		}
		else {
			if (d1[i] > d2[i])a.insert({ d1[i],i + 1,s });
			else a.insert({ d2[i],i + 1,t });
			ans += max(d1[i], d2[i]);
		}
	}
	cout << ans << "\n";
	for (auto itr = a.rbegin(); itr != a.rend(); itr++) {
		auto [w, u, v] = *itr;
		cout << u << " " << v << " " << u << "\n";
	}
	for (auto itr = b.rbegin(); itr != b.rend(); itr++) {
		auto [w, u, v] = *itr;
		cout << u << " " << v << " " << u << "\n";
	}

	return 0;
}


#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+500;

int gti(void)
{
    char c=getchar();
    int ret=0,st=1;
    for (;!isdigit(c);c=getchar()) if (c=='-') st=-1;
    for (;isdigit(c);c=getchar()) ret=ret*10+c-'0';
    return ret*st;
}

struct edge{edge *nxt;int to;}pool[N*2],*pt[N],*p=pool;
void add(int a,int b)
{
    *(++p)=(edge){pt[a],b},pt[a]=p;
    *(++p)=(edge){pt[b],a},pt[b]=p;
}

int dep[N],f[N];
int dfs1(int v,int fa=0)
{
    int ret=v;
    f[v]=fa,dep[v]=dep[fa]+1;
    for (edge *i=pt[v];i;i=i->nxt)
        if (i->to!=fa)
        {
            int now=dfs1(i->to,v);
            if (dep[now]>dep[ret])
                ret=now;
        }
    return ret;
}

struct A
{
    int x,y,z;
}ans[N],ans2[N];
int tot,tot2,sum;

void dfs2(int v,int fa,int fr,int val)
{
    for (edge *i=pt[v];i;i=i->nxt)
        if (i->to!=fa)
            dfs2(i->to,v,fr,val+1);
    ans[++tot]=(A){fr,v,v};
    sum+=val;
}

int main(void)
{
    int n=gti();
    for (int i=1;i<n;i++)
    {
        int u=gti(),v=gti();
        add(u,v);
    }
    int p1=dfs1(1),p2=dfs1(p1);
    int fr=p1,lst=p2;
    for (int i=p2;i;i=f[i])
    {
        if (dep[p2]-dep[i]>dep[i]-dep[p1])
            fr=p2;
        for (edge *j=pt[i];j;j=j->nxt)
            if (j->to!=f[i]&&j->to!=lst)
                dfs2(j->to,i,fr,abs(dep[fr]-dep[i])+1);
        ans2[++tot2]=(A){p1,i,i};
        sum+=abs(dep[i]-dep[p1]);
        lst=i;
    }
    printf("%d\n",sum);
    for (int i=1;i<=tot;i++)
        printf("%d %d %d\n",ans[i].x,ans[i].y,ans[i].z);
    for (int i=1;i<tot2;i++)
        printf("%d %d %d\n",ans2[i].x,ans2[i].y,ans2[i].z);
    return 0;
}


#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+500;
#define int long long

int gti(void)
{
    char c=getchar();
    int ret=0,st=1;
    for (;!isdigit(c);c=getchar()) if (c=='-') st=-1;
    for (;isdigit(c);c=getchar()) ret=ret*10+c-'0';
    return ret*st;
}

struct edge{edge *nxt;int to;}pool[N*2],*pt[N],*p=pool;
void add(int a,int b)
{
    *(++p)=(edge){pt[a],b},pt[a]=p;
    *(++p)=(edge){pt[b],a},pt[b]=p;
}

int dep[N],f[N];
int dfs1(int v,int fa=0)
{
    int ret=v;
    f[v]=fa,dep[v]=dep[fa]+1;
    for (edge *i=pt[v];i;i=i->nxt)
        if (i->to!=fa)
        {
            int now=dfs1(i->to,v);
            if (dep[now]>dep[ret])
                ret=now;
        }
    return ret;
}

struct A
{
    int x,y,z;
}ans[N],ans2[N];
int tot,tot2,sum;

void dfs2(int v,int fa,int fr,int val)
{
    for (edge *i=pt[v];i;i=i->nxt)
        if (i->to!=fa)
            dfs2(i->to,v,fr,val+1);
    ans[++tot]=(A){fr,v,v};
    sum+=val;
}

#undef int
int main(void)
#define int long long
{
    int n=gti();
    for (int i=1;i<n;i++)
    {
        int u=gti(),v=gti();
        add(u,v);
    }
    int p1=dfs1(1),p2=dfs1(p1);
    int fr=p1,lst=p2;
    for (int i=p2;i;i=f[i])
    {
        if (dep[p2]-dep[i]>dep[i]-dep[p1])
            fr=p2;
        for (edge *j=pt[i];j;j=j->nxt)
            if (j->to!=f[i]&&j->to!=lst)
                dfs2(j->to,i,fr,abs(dep[fr]-dep[i])+1);
        ans2[++tot2]=(A){p1,i,i};
        sum+=abs(dep[i]-dep[p1]);
        lst=i;
    }
    printf("%lld\n",sum);
    for (int i=1;i<=tot;i++)
        printf("%lld %lld %lld\n",ans[i].x,ans[i].y,ans[i].z);
    for (int i=1;i<tot2;i++)
        printf("%lld %lld %lld\n",ans2[i].x,ans2[i].y,ans2[i].z);
    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(mdepth[a] == mdepth[b]){
        if(depth[a] == depth[b]){
            return sibs[a] < sibs[b];
        }
        else return depth[a] < depth[b];
    }
    else return mdepth[a] > mdepth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int p1 = 0, p2 = 0;
    int d1 = rt, d2 = rt;
    for(int i = 1; i < n; ++i){
        p1 = d1; p2 = d2;
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(max(a, b) >= dist(d1, d2)){
            if(a > b){
                d2 = nodes[i];
            }
            else{
                d1 = nodes[i];
            }
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        if(d1 != p1){
            ans[i - 1].y = d1;
        }
        else{
            ans[i - 1].y = d2;
        }
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(mdepth[a] == mdepth[b]){
        if(depth[a] == depth[b]){
            return sibs[a] < sibs[b];
        }
        else return depth[a] < depth[b];
    }
    else return mdepth[a] > mdepth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = rt, d2 = rt;
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(max(a, b) >= dist(d1, d2)){
            if(a > b){
                d2 = nodes[i];
            }
            else{
                d1 = nodes[i];
            }
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(depth[a] == depth[b]){
        return sibs[a] < sibs[b];
    }
    else return depth[a] < depth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = nodes[0], d2 = nodes[0];
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            d2 = nodes[i];
        }
        else{
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN];
vector<pair<pii, int>> ans;
set<int> ondiam;
int d1, d2;
ll amt = 0;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            ondiam.insert(v);
        }
        findpath(i, v);
    }
}
void deleteothers(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        deleteothers(i, v);
    }
    if(ondiam.find(v) == ondiam.end()){
        int a = dist(v, d1);
        int b = dist(v, d2);
        if(a > b){
            ans.eb(mp(mp(v, d1), v));
            amt += a;
        }
        else{
            ans.eb(mp(mp(v, d2), v));
            amt += b;
        }
    }
}
void deletediam(int v = d2, int p = -1){
    if(ondiam.find(v) == ondiam.end() || v == d1) return;
    ans.eb(mp(mp(v, d1), v));
    amt += dist(v, d1);
    for(int i : adj[v]){
        if(i == p) continue;
        deletediam(i, v);
    }
}
int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    dfsdiam(rt, -1);
    d1 = rt;
    mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    fill(depth, depth + n, 0);
    initialize(d1);
    findpath();
    ondiam.insert(d1);
    ondiam.insert(d2);
    deleteothers();
    deletediam();
    cout << amt << "\n";
    for(auto i : ans){
        cout << i.x.x + 1 << " " << i.x.y + 1 << " " << i.y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(depth[a] == depth[b]){
        return sibs[a] < sibs[b];
    }
    else return depth[a] > depth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = rt, d2 = rt;
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            d2 = nodes[i];
        }
        else{
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 18;

int depth[mxN];
vector<pair<pii, int>> ans;
set<int> ondiam;
int d1, d2;
ll amt = 0;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            ondiam.insert(v);
        }
        findpath(i, v);
    }
}
void deleteothers(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        deleteothers(i, v);
    }
    if(ondiam.find(v) == ondiam.end()){
        int a = dist(v, d1);
        int b = dist(v, d2);
        if(a > b){
            ans.eb(mp(mp(v, d1), v));
            amt += a;
        }
        else{
            ans.eb(mp(mp(v, d2), v));
            amt += b;
        }
    }
}
void deletediam(int v = d2, int p = -1){
    if(ondiam.find(v) == ondiam.end() || v == d1) return;
    ans.eb(mp(mp(v, d1), v));
    amt += dist(v, d1);
    for(int i : adj[v]){
        if(i == p) continue;
        deletediam(i, v);
    }
}
int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    d1 = rt;
    dfsdiam(rt, -1);
    mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    fill(depth, depth + n, 0);
    initialize(d1);
    findpath();
    ondiam.insert(d1);
    ondiam.insert(d2);
    deleteothers();
    deletediam();
    cout << amt << "\n";
    for(auto i : ans){
        cout << i.x.x + 1 << " " << i.x.y + 1 << " " << i.y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 18;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> nodes;
int d1, d2;
set<int> ondiam;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            nodes.eb(i);
            ondiam.insert(i);
        }
        findpath(i, v);
    }
}
void restorder(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(ondiam.find(i) == ondiam.end()){
            nodes.eb(i);
        }
        restorder(i, v);
    }
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    d1 = rt;
    dfsdiam(d1, -1);
    mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    initialize(rt);
    nodes.eb(d1);
    ondiam.insert(d1);
    findpath();
    restorder();
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    d2 = rt;
    int cur2 = -1;
    for(int i = 1; i < n; ++i){
        if(cur2 == -1){
            int a = dist(d1, nodes[i]);
            int b = 0;
            if(a > b){
                amt += dist(d1, nodes[i]);
                ans[i - 1].x.x = d1;
                ans[i - 1].x.y = nodes[i];
                ans[i - 1].y = nodes[i];
            }
            else{
                amt += dist(nodes[i], d2);
                ans[i - 1].x.x = d2;
                ans[i - 1].x.y = nodes[i];
                ans[i - 1].y = nodes[i];
            }
            if(nodes[i] == d2) cur2 = d2;
            continue;
        }
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            amt += dist(d1, nodes[i]);
            ans[i - 1].x.x = d1;
            ans[i - 1].x.y = nodes[i];
            ans[i - 1].y = nodes[i];
        }
        else{
            amt += dist(nodes[i], d2);
            ans[i - 1].x.x = d2;
            ans[i - 1].x.y = nodes[i];
            ans[i - 1].y = nodes[i];
        }
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(mdepth[a] == mdepth[b]){
        if(depth[a] == depth[b]){
            return sibs[a] < sibs[b];
        }
        else return depth[a] < depth[b];
    }
    else return mdepth[a] > mdepth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = rt, d2 = rt;
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            d2 = nodes[i];
        }
        else{
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN];
vector<pair<pii, int>> ans;
set<int> ondiam;
int d1, d2;
ll amt = 0;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            ondiam.insert(v);
        }
        findpath(i, v);
    }
}
void deletediam(int v = d2, int p = -1){
    if(ondiam.find(v) == ondiam.end() || v == d1) return;
    ans.eb(mp(mp(v, d1), v));
    amt += dist(v, d1);
    for(int i : adj[v]){
        if(i == p) continue;
        deletediam(i, v);
    }
}
int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    dfsdiam(rt, -1);
    d1 = rt;
    mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    fill(depth, depth + n, 0);
    initialize(d1);
    findpath();
    ondiam.insert(d1);
    ondiam.insert(d2);
    for(int i = 0; i < n; ++i){
        if(ondiam.find(i) == ondiam.end()){
            int a = dist(i, d1);
            int b = dist(i, d2);
            if(a > b){
                ans.eb(mp(mp(i, d1), i));
                amt += a;
            }
            else{
                ans.eb(mp(mp(i, d2), i));
                amt += b;
            }
        }
    }
    deletediam();
    cout << amt << "\n";
    for(auto i : ans){
        cout << i.x.x + 1 << " " << i.x.y + 1 << " " << i.y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsInitialize(int v, int p) {
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
    }
}
void initialize() {
    dfsInitialize(0, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    return depth[a] == depth[b] ? sibs[a] < sibs[b] : depth[a] < depth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    initialize();
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int p1 = 0, p2 = 0;
    int d1 = 0, d2 = 0;
    for(int i = 1; i < n; ++i){
        p1 = d1; p2 = d2;
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(max(a, b) >= dist(d1, d2)){
            if(a > b){
                d2 = nodes[i];
            }
            else{
                d1 = nodes[i];
            }
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        if(d1 != p1){
            ans[i - 1].y = d1;
        }
        else{
            ans[i - 1].y = d2;
        }
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 18;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> nodes;
int d1, d2;
set<int> ondiam;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            nodes.eb(i);
            ondiam.insert(i);
        }
        findpath(i, v);
    }
}
void restorder(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(ondiam.find(v) == ondiam.end()){
            nodes.eb(i);
        }
        restorder(i, v);
    }
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    d1 = rt;
    dfsdiam(d1, -1);
    mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    initialize(rt);
    nodes.eb(d1);
    ondiam.insert(d1);
    findpath();
    restorder();
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    d2 = rt;
    int cur2 = -1;
    for(int i = 1; i < n; ++i){
        if(cur2 == -1){
            int a = dist(d1, nodes[i]);
            int b = 0;
            if(a > b){
                amt += dist(d1, nodes[i]);
                ans[i - 1].x.x = d1;
                ans[i - 1].x.y = nodes[i];
                ans[i - 1].y = nodes[i];
            }
            else{
                amt += dist(nodes[i], d2);
                ans[i - 1].x.x = d2;
                ans[i - 1].x.y = nodes[i];
                ans[i - 1].y = nodes[i];
            }
            if(nodes[i] == d2) cur2 = d2;
            continue;
        }
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            amt += dist(d1, nodes[i]);
            ans[i - 1].x.x = d1;
            ans[i - 1].x.y = nodes[i];
            ans[i - 1].y = nodes[i];
        }
        else{
            amt += dist(nodes[i], d2);
            ans[i - 1].x.x = d2;
            ans[i - 1].x.y = nodes[i];
            ans[i - 1].y = nodes[i];
        }
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(mdepth[a] == mdepth[b]){
        return sibs[a] < sibs[b];
    }
    else return mdepth[a] > mdepth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = rt, d2 = rt;
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            d2 = nodes[i];
        }
        else{
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsInitialize(int v, int p) {
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
    }
}
void initialize() {
    dfsInitialize(0, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    return depth[a] == depth[b] ? sibs[a] < sibs[b] : depth[a] < depth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    initialize();
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int p1 = 0, p2 = 0;
    int d1 = 0, d2 = 0;
    for(int i = 1; i < n; ++i){
        p1 = d1; p2 = d2;
        if(dist(d1, nodes[i]) > dist(d1, d2)){
            d2 = nodes[i];
        }
        if(dist(d2, nodes[i]) > dist(d1, d2)){
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        if(d1 != p1){
            ans[i - 1].y = d1;
        }
        else{
            ans[i - 1].y = d2;
        }
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(mdepth[a] == mdepth[b]){
        if(sibs[a] == sibs[b]){
            return depth[a] < depth[b];
        }
        else return sibs[a] < sibs[b];
    }
    else return mdepth[a] > mdepth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = rt, d2 = rt;
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            d2 = nodes[i];
        }
        else{
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN];
vector<pair<pii, int>> ans;
set<int> ondiam;
int d1, d2;
ll amt = 0;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            ondiam.insert(v);
        }
        findpath(i, v);
    }
}
void deleteothers(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        deleteothers(i, v);
    }
    if(ondiam.find(v) == ondiam.end()){
        int a = dist(v, d1);
        int b = dist(v, d2);
        if(a > b){
            ans.eb(mp(mp(v, d1), v));
            amt += a;
        }
        else{
            ans.eb(mp(mp(v, d2), v));
            amt += b;
        }
    }
}
void deletediam(int v = d2, int p = -1){
    if(ondiam.find(v) == ondiam.end() || v == d1) return;
    ans.eb(mp(mp(v, d1), v));
    amt += dist(v, d1);
    for(int i : adj[v]){
        if(i == p) continue;
        deletediam(i, v);
    }
}
int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    dfsdiam(rt, -1);
    d1 = rt;
    mx = 0;
    for(int i= 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    fill(depth, depth + n, 0);
    initialize(d1);
    findpath();
    ondiam.insert(d1);
    ondiam.insert(d2);
    deleteothers();
    deletediam();
    cout << amt << "\n";
    for(auto i : ans){
        cout << i.x.x + 1 << " " << i.x.y + 1 << " " << i.y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 18;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> nodes;
int d1, d2;
set<int> ondiam;
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}
void findpath(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(dist(i, d2) < dist(v, d2)){
            nodes.eb(i);
            ondiam.insert(i);
        }
        findpath(i, v);
    }
}
void restorder(int v = d1, int p = -1){
    for(int i : adj[v]){
        if(i == p) continue;
        if(ondiam.find(v) == ondiam.end()){
            nodes.eb(i);
        }
        restorder(i, v);
    }
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    d1 = rt;
    dfsdiam(d1, -1);
    mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    d2 = rt;
    initialize(rt);
    nodes.eb(d1);
    ondiam.insert(d1);
    findpath();
    restorder();
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    d2 = rt;
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            amt += dist(d1, nodes[i]);
            ans[i - 1].x.x = d1;
            ans[i - 1].x.y = nodes[i];
            ans[i - 1].y = nodes[i];
        }
        else{
            amt += dist(nodes[i], d2);
            ans[i - 1].x.x = d2;
            ans[i - 1].x.y = nodes[i];
            ans[i - 1].y = nodes[i];
        }
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

int add(int a, int b){
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

const int mxN = 2e5 + 5;
const int mxD = 17;

int depth[mxN], sibs[mxN], mdepth[mxN];
vector<int> adj[mxN];
int bLift[mxN][mxD];
void dfsdiam(int v = 0, int p = -1){
    for(int i: adj[v]) {
        if(i == p)
            continue;
        depth[i] = depth[v] + 1;
        dfsdiam(i, v);
    }
}
void dfsInitialize(int v, int p) {
    mdepth[v] = depth[v];
    for(int i: adj[v]) {
        if(i == p)
            continue;
        sibs[i] = adj[v].size();
        depth[i] = depth[v] + 1;
        bLift[i][0] = v;
        dfsInitialize(i, v);
        mdepth[v] = max(mdepth[v], mdepth[i]);
    }
}
void initialize(int r) {
    dfsInitialize(r, -1);
    for(int d = 1; d < mxD; d++) {
        for(int i = 0; i < mxN; i++) {
            bLift[i][d] = bLift[bLift[i][d-1]][d-1];
        }
    }
}
int LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(depth[a] - (1<<d) >= depth[b]) {
            a = bLift[a][d];
        }
    }
    for(int d = mxD-1; d >= 0; d--) {
        if(bLift[a][d] != bLift[b][d]) {
            a = bLift[a][d];
            b = bLift[b][d];
        }
    }
    if(a != b) {
        a = bLift[a][0];
        b = bLift[b][0];
    }
    return a;
}
int dist(int a, int b){
    int lca = LCA(a, b);
    return depth[a] - depth[lca] + depth[b] - depth[lca];
}

bool comp(int a, int b){
    if(depth[a] == depth[b]){
        return sibs[a] < sibs[b];
    }
    else return depth[a] > depth[b];
}

int main(){
    setIO();
    int n; cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfsdiam();
    int mx = 0;
    for(int i = 0; i < n; ++i){
        mx = max(mx, depth[i]);
    }
    int rt = 0;
    for(int i = 0; i < n; ++i){
        if(depth[i] == mx) {
            rt = i;
            break;
        }
    }
    fill(depth, depth + n, 0);
    initialize(rt);
    int nodes[n];
    for(int i = 0; i < n; ++i){
        nodes[i] = i;
    }
    sort(nodes, nodes + n, comp);
    pair<pii, int> ans[n - 1];
    ll amt = 0;
    int d1 = nodes[0], d2 = nodes[0];
    for(int i = 1; i < n; ++i){
        int a = dist(d1, nodes[i]);
        int b = dist(d2, nodes[i]);
        if(a > b){
            d2 = nodes[i];
        }
        else{
            d1 = nodes[i];
        }
        amt += dist(d1, d2);
        ans[i - 1].x.x = d1;
        ans[i - 1].x.y = d2;
        ans[i - 1].y = nodes[i];
    }
    cout << amt << "\n";
    for(int i = n - 2; i >= 0; --i){
        cout << ans[i].x.x + 1 << " " << ans[i].x.y + 1 << " " << ans[i].y + 1 << "\n";
    }
}


#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
int64 sum[N], dep[N], sz[N];
vector<int> a[N];

void DFS(int u, int parent) {
  dep[u] = parent < 0 ? 0 : dep[parent] + 1;
  sz[u] = 1;
  sum[u] = dep[u];
  for (auto& v : a[u]) {
    if (v == parent) continue;
    DFS(v, u);
    sz[u] += sz[v];
    sum[u] += sum[v];
  }
}

int64 best, best_i;
int n;
void solve(int u, int parent, int64 up) {
  int64 cur = sum[u] - sz[u] * dep[u] + up;
  // trace(u, cur, up);
  if (best < cur) {
    best = cur;
    best_i = u;
  }
  for (auto& v : a[u]) {
    if (v == parent) continue;
    int64 other = sum[u] - sz[u] * dep[u] - (sum[v] - sz[v] * dep[v] + sz[v]);
    // trace(u, v, other);
    int64 nxt_up = other + up + (n - sz[v]);
    solve(v, u, nxt_up);
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  DFS(0, -1);
  best = 0;
  solve(0, -1, 0);
  // trace(best, best_i);

  cout << best << '\n';
  vector<int> b;
  for (int i = 0; i < n; ++i) {
    if (i != best_i) b.push_back(i);
  }
  sort(b.begin(), b.end(),
       [&](int x, int y) {
         return dep[x] > dep[y];
       });
  for (auto& i : b) {
    cout << i + 1 << " " << best_i + 1 << " " << i + 1 << '\n';
  }

  return 0;
}


// #define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
vector<int> a[N];
int pre[N];

int BFS(int s, int n) {
  fill(pre, pre + n, -1);
  pre[s] = s;
  queue<int> Q;
  Q.push(s);
  int ret = s;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    trace("BFS", u);
    for (auto& v : a[u]) {
      if (pre[v] >= 0) continue;
      pre[v] = u;
      Q.push(v);
      ret = v;
    }
  }
  return ret;
}

void solve(int s, vector<int>& d) {
  fill(d.begin(), d.end(), -1);
  d[s] = 0;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (auto& v : a[u]) {
      if (d[v] >= 0) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  int s = BFS(0, n);
  trace(s);
  int t = BFS(s, n);

  trace(s, t);
  vector<int> ds(n), dt(n);
  solve(s, ds);
  solve(t, dt);
  vector<bool> visit(n);
  vector<int> path;
  for (int x = t; ; x = pre[x]) {
    visit[x] = true;
    path.push_back(x);
    if (x == s) break;
  }
  trace(path);

  vector<int> d(n, -1);

  int64 ret = 0;
  vector<array<int, 3>> rec;
  for (int i = 0; i < n; ++i) {
    if (visit[i]) continue;
    if (ds[i] > dt[i]) {
      ret += ds[i];
      rec.push_back({i, s, i});
    } else {
      ret += dt[i];
      rec.push_back({i, t, i});
    }
  }
  int64 len = SZ(path);
  ret += len * (len - 1) / 2;
  cout << ret << '\n';

  for (auto& v : rec) {
    cout << v[0] + 1 << " " << v[1] + 1 << " " << v[2] + 1 << '\n';
  }
  for (int i = 0; i + 1 < SZ(path); ++i) {
    cout << path[i] + 1 << " " << path.back() + 1 << " " << path[i] + 1 << '\n';
  }
  return 0;
}


#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
int64 sum[N], dep[N], sz[N];
vector<int> a[N];

void DFS(int u, int parent) {
  dep[u] = parent < 0 ? 0 : dep[parent] + 1;
  sz[u] = 1;
  sum[u] = dep[u];
  for (auto& v : a[u]) {
    if (v == parent) continue;
    DFS(v, u);
    sz[u] += sz[v];
    sum[u] += sum[v];
  }
}

int64 best, best_i;
int n;
void solve(int u, int parent, int64 up) {
  int64 cur = sum[u] - sz[u] * dep[u] + up;
  // trace(u, cur, up);
  if (SZ(a[u]) == 1 && best < cur) {
    best = cur;
    best_i = u;
  }
  for (auto& v : a[u]) {
    if (v == parent) continue;
    int64 other = sum[u] - sz[u] * dep[u] - (sum[v] - sz[v] * dep[v] + sz[v]);
    // trace(u, v, other);
    int64 nxt_up = other + up + (n - sz[v]);
    solve(v, u, nxt_up);
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  DFS(0, -1);
  best = 0;
  solve(0, -1, 0);
  // trace(best, best_i);

  cout << best << '\n';
  vector<int> b;
  for (int i = 0; i < n; ++i) {
    if (i != best_i) b.push_back(i);
  }
  sort(b.begin(), b.end(),
       [&](int x, int y) {
         return dep[x] > dep[y];
       });
  for (auto& i : b) {
    cout << i + 1 << " " << best_i + 1 << " " << i + 1 << '\n';
  }

  return 0;
}


#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
vector<int> a[N];
int pre[N];

int BFS(int s, int n) {
  fill(pre, pre + n, -1);
  pre[s] = s;
  queue<int> Q;
  Q.push(s);
  int ret = s;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (auto& v : a[u]) {
      if (pre[v] >= 0) continue;
      pre[v] = u;
      ret = v;
    }
  }
  return ret;
}

void solve(int s, vector<int>& d) {
  fill(d.begin(), d.end(), -1);
  d[s] = 0;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (auto& v : a[u]) {
      if (d[v] >= 0) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  int s = BFS(0, n);
  int t = BFS(s, n);

  vector<int> ds(n), dt(n);
  solve(s, ds);
  solve(t, dt);
  vector<bool> visit(n);
  visit[s] = true;
  vector<int> path(1, s);
  for (int x = t; x != s; x = pre[x]) {
    visit[x] = true;
    path.push_back(x);
  }

  vector<int> d(n, -1);

  int64 ret = 0;
  vector<array<int, 3>> rec;
  for (int i = 0; i < n; ++i) {
    if (visit[i]) continue;
    if (ds[i] > dt[i]) {
      ret += ds[i];
      rec.push_back({i, s, i});
    } else {
      ret += dt[i];
      rec.push_back({i, t, i});
    }
  }
  int64 len = SZ(path);
  ret += len * (len - 1) / 2;
  cout << ret << '\n';

  for (auto& v : rec) {
    cout << v[0] + 1 << " " << v[1] + 1 << " " << v[2] + 1 << '\n';
  }
  for (int i = 0; i + 1 < SZ(path); ++i) {
    cout << path[i] + 1 << " " << path.back() + 1 << " " << path[i] + 1 << '\n';
  }
  return 0;
}


// #define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
vector<int> a[N];
int pre[N];

int BFS(int s, int n) {
  fill(pre, pre + n, -1);
  pre[s] = s;
  queue<int> Q;
  Q.push(s);
  int ret = s;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    trace("BFS", u);
    for (auto& v : a[u]) {
      if (pre[v] >= 0) continue;
      pre[v] = u;
      Q.push(v);
      ret = v;
    }
  }
  return ret;
}

void solve(int s, vector<int>& d) {
  fill(d.begin(), d.end(), -1);
  d[s] = 0;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (auto& v : a[u]) {
      if (d[v] >= 0) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  int s = BFS(0, n);
  trace(s);
  int t = BFS(s, n);

  trace(s, t);
  vector<int> ds(n), dt(n);
  solve(s, ds);
  solve(t, dt);
  vector<bool> visit(n);
  vector<int> path;
  for (int x = t; ; x = pre[x]) {
    visit[x] = true;
    path.push_back(x);
    if (x == s) break;
  }
  trace(path);

  vector<int> d(n, -1);

  int64 ret = 0;
  vector<array<int, 3>> rec;
  vector<int> p(n);
  for (int i = 0; i < n; ++i) p[i] = i;
  sort(p.begin(), p.end(),
       [&](int u, int v) {
         return ds[u] > ds[v];
       });
  for (int k = 0; k < n; ++k) {
    int i = p[k];
    if (visit[i]) continue;
    if (ds[i] > dt[i]) {
      ret += ds[i];
      rec.push_back({i, s, i});
    } else {
      ret += dt[i];
      rec.push_back({i, t, i});
    }
  }
  int64 len = SZ(path);
  ret += len * (len - 1) / 2;
  cout << ret << '\n';

  for (auto& v : rec) {
    cout << v[0] + 1 << " " << v[1] + 1 << " " << v[2] + 1 << '\n';
  }
  for (int i = 0; i + 1 < SZ(path); ++i) {
    cout << path[i] + 1 << " " << path.back() + 1 << " " << path[i] + 1 << '\n';
  }
  return 0;
}