Problem: You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps: 

  1. choose two leaves; 
  2. add the length of the simple path between them to the answer; 
  3. remove one of the chosen leaves from the tree. 



Initial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex. 

Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!

Input

The first line contains one integer number n (2 ≤ n ≤ 2·105) — the number of vertices in the tree. 

Next n - 1 lines describe the edges of the tree in form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that given graph is a tree.

Output

In the first line print one integer number — maximal possible answer. 

In the next n - 1 lines print the operations in order of their applying in format ai, bi, ci, where ai, bi — pair of the leaves that are chosen in the current operation (1 ≤ ai, bi ≤ n), ci (1 ≤ ci ≤ n, ci = ai or ci = bi) — choosen leaf that is removed from the tree in the current operation. 

See the examples for better understanding.

Examples

Input

3
1 2
1 3


Output

3
2 3 3
2 1 1


Input

5
1 2
1 3
2 4
2 5


Output

9
3 5 5
4 3 3
4 1 1
4 2 2

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Pradyumn
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            Debug debug = new Debug(out);
            int n = in.nextInt();
            TaskF.Graph graph = new TaskF.Graph(n);
            for (int i = 0; i < n - 1; ++i) {
                graph.addEdge(in.nextInt() - 1, in.nextInt() - 1);
            }
            graph.proc(); // processed graph


            int[] deg = graph.deg;
            boolean[] visit = new boolean[n];
            MinHeap minHeap = new MinHeap(n);
            for (int i = 0; i < n; ++i) {
                if (i == graph.firstEnd || i == graph.secondEnd) continue;
                minHeap.add(i, deg[i]);
            }

            //all nodes apart from diameter route are removed first
            long ans = 0;
            ArrayList<String> ret = new ArrayList<>();
            while (minHeap.size() > 0) {
                int cur = minHeap.argmin();
                int val = minHeap.remove(cur);
                if (val > 1) break;
                visit[cur] = true;

                int d1 = graph.distanceLCA(graph.firstEnd, cur);
                int d2 = graph.distanceLCA(graph.secondEnd, cur);
                ans += Math.max(d1, d2);
                if (d1 > d2) {
                    ret.add((graph.firstEnd + 1) + " " + (cur + 1) + " " + (cur + 1));
                } else {
                    ret.add((graph.secondEnd + 1) + " " + (cur + 1) + " " + (cur + 1));
                }
                for (Integer next : graph.g[cur]) {
                    if (next == graph.secondEnd || next == graph.firstEnd) continue;
                    if (!visit[next]) {
                        minHeap.update(next, --deg[next]);
                    }
                }
            }

            // all nodes lying in the path of diameter are removed
            for (int cur = graph.secondEnd; cur != graph.firstEnd; ) {
                ret.add((graph.firstEnd + 1) + " " + (cur + 1) + " " + (cur + 1));
                ans += graph.distanceLCA(graph.firstEnd, cur);
                visit[cur] = true;
                for (Integer next : graph.g[cur]) {
                    if (!visit[next]) {
                        cur = next;
                        break;
                    }
                }
            }
            out.println(ans);
            for (String ss : ret) out.println(ss);
        }

        static class Graph {
            LCASparseTable lcaSparseTable;
            List<Integer>[] g;
            int[] depth;
            int[] parent;
            int[] diameter;
            int[] deg;
            int firstEnd;
            int secondEnd;
            int n;

            public Graph(int n) {
                this.n = n;
                g = new List[n];
                for (int i = 0; i < n; ++i)
                    g[i] = new ArrayList<>();
                depth = new int[n];
                parent = new int[n];
                deg = new int[n];
                ArrayUtils.fill(parent, -1);
            }

            public void addEdge(int u, int v) {
                g[u].add(v);
                g[v].add(u);
            }

            public void proc() {
                //LCA sparse table cal
                lcaSparseTable = new LCASparseTable(g, 0);
                int d01 = -1;
                {
                    // bfs for diameter and depth, parent
                    int[] Q = new int[n];
                    boolean[] visit = new boolean[n];
                    int sizeQ = 0;
                    Q[sizeQ++] = 0;
                    for (int i = 0; i < sizeQ; ++i) {
                        int cur = Q[i];
                        visit[cur] = true;
                        for (Integer next : g[cur]) {
                            if (!visit[next]) {
                                depth[next] = depth[cur] + 1;
                                parent[next] = cur;
                                Q[sizeQ++] = next;
                            }
                        }
                    }
                    firstEnd = Q[sizeQ - 1];
                }
                {
                    //final bfs for diameter
                    int[] Q = new int[n];
                    int[] d = new int[n];
                    boolean[] visit = new boolean[n];
                    int sizeQ = 0;
                    Q[sizeQ++] = firstEnd;
                    for (int i = 0; i < sizeQ; ++i) {
                        int cur = Q[i];
                        visit[cur] = true;
                        for (Integer next : g[cur]) {
                            if (!visit[next]) {
                                d[next] = d[cur] + 1;
                                Q[sizeQ++] = next;
                            }
                        }
                    }
                    secondEnd = Q[sizeQ - 1];
                    d01 = d[secondEnd];
                }
                {
                    for (int i = 0; i < n; ++i)
                        deg[i] = g[i].size();
                }
                diameter = new int[]{d01, firstEnd, secondEnd};
            }

            public int distanceLCA(int x, int y) {
                int lca = lcaSparseTable.LowestCommonAncestor(x, y);
                return depth[x] + depth[y] - 2 * depth[lca];
            }

        }

    }

    static class LCASparseTable {
        public int len;
        public int[][] up;
        public int[] tin;
        public int[] tout;
        public int time;

        public LCASparseTable(List<Integer>[] tree, int root) {
            int n = tree.length;
            len = 1;
            while ((1 << len) <= n) ++len;
            up = new int[len][n];
            tin = new int[n];
            tout = new int[n];
            dfs(tree, root, root);
        }

        public void dfs(List<Integer>[] tree, int u, int p) {
            tin[u] = time++;
            up[0][u] = p;
            for (int i = 1; i < len; i++) {
                up[i][u] = up[i - 1][up[i - 1][u]];
            }
            for (int v : tree[u]) {
                if (v != p) {
                    dfs(tree, v, u);
                }
            }
            tout[u] = time++;
        }

        public boolean isParent(int parent, int child) {
            return tin[parent] <= tin[child] && tout[child] <= tout[parent];
        }

        public int LowestCommonAncestor(int a, int b) {
            if (isParent(a, b)) {
                return a;
            }
            if (isParent(b, a)) {
                return b;
            }
            for (int i = len - 1; i >= 0; i--) {
                if (!isParent(up[i][a], b)) {
                    a = up[i][a];
                }
            }
            return up[0][a];
        }

    }

    static class MinHeap {
        public int[] a;
        public int[] map;
        public int[] imap;
        public int n;
        public int pos;
        public static int INF = Integer.MAX_VALUE;

        public MinHeap(int m) {
            n = m + 2;
            a = new int[n];
            map = new int[n];
            imap = new int[n];
            Arrays.fill(a, INF);
            Arrays.fill(map, -1);
            Arrays.fill(imap, -1);
            pos = 1;
        }

        public int add(int ind, int x) {
            int ret = imap[ind];
            if (imap[ind] < 0) {
                a[pos] = x;
                map[pos] = ind;
                imap[ind] = pos;
                pos++;
                up(pos - 1);
            }
            return ret != -1 ? a[ret] : x;
        }

        public int update(int ind, int x) {
            int ret = imap[ind];
            if (imap[ind] < 0) {
                a[pos] = x;
                map[pos] = ind;
                imap[ind] = pos;
                pos++;
                up(pos - 1);
            } else {
                int o = a[ret];
                a[ret] = x;
                up(ret);
                down(ret);
//              if(a[ret] > o){
//                  up(ret);
//              }else{
//                  down(ret);
//              }
            }
            return x;
        }

        public int remove(int ind) {
            if (pos == 1) return INF;
            if (imap[ind] == -1) return INF;

            pos--;
            int rem = imap[ind];
            int ret = a[rem];
            map[rem] = map[pos];
            imap[map[pos]] = rem;
            imap[ind] = -1;
            a[rem] = a[pos];
            a[pos] = INF;
            map[pos] = -1;

            up(rem);
            down(rem);
            return ret;
        }

        public int argmin() {
            return map[1];
        }

        public int size() {
            return pos - 1;
        }

        private void up(int cur) {
            for (int c = cur, p = c >>> 1; p >= 1 && a[p] > a[c]; c >>>= 1, p >>>= 1) {
                int d = a[p];
                a[p] = a[c];
                a[c] = d;
                int e = imap[map[p]];
                imap[map[p]] = imap[map[c]];
                imap[map[c]] = e;
                e = map[p];
                map[p] = map[c];
                map[c] = e;
            }
        }

        private void down(int cur) {
            for (int c = cur; 2 * c < pos; ) {
                int b = a[2 * c] < a[2 * c + 1] ? 2 * c : 2 * c + 1;
                if (a[b] < a[c]) {
                    int d = a[c];
                    a[c] = a[b];
                    a[b] = d;
                    int e = imap[map[c]];
                    imap[map[c]] = imap[map[b]];
                    imap[map[b]] = e;
                    e = map[c];
                    map[c] = map[b];
                    map[b] = e;
                    c = b;
                } else {
                    break;
                }
            }
        }

    }

    static class Debug {
        PrintWriter out;
        boolean oj;
        long timeBegin;
        Runtime runtime;

        public Debug(PrintWriter out) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
            this.out = out;
            this.timeBegin = System.currentTimeMillis();
            this.runtime = Runtime.getRuntime();
        }

    }

    static class ArrayUtils {
        public static void fill(int[] array, int value) {
            Arrays.fill(array, value);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int pnumChars;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        private int pread() {
            if (pnumChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= pnumChars) {
                curChar = 0;
                try {
                    pnumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (pnumChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = pread();
            while (isSpaceChar(c))
                c = pread();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = pread();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = pread();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = pread();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}