Problem: Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.

Input

The first line contains integer n (2 ≤ n ≤ 1000) — amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself.

Output

Output the answer, number t — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.

Examples

Input

2
1 2


Output

0


Input

7
1 2
2 3
3 1
4 5
5 6
6 7


Output

1
3 1 3 7

Solution (in JAVA):

Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Roads_not_only_in_Berland {

    class link {
        public int linkNum, count;

        public link(int lNum, int cnt) {
            this.linkNum = lNum;
            this.count = cnt;
        }

        @Override
        public String toString() {
            return linkNum / n + " " + linkNum % n + " " + count;
        }

    }

    public int[] colour;
    public int[] dfsNum;
    public int num;

    public void clearBridges() {
        // find all bridges
        findAllBridges();

        // clear bridges from links list
        for (ArrayList<link> ll : links) {
            if (ll == null)
                continue;
            for (int i = 0; i < ll.size(); i++) {
                link tmp = ll.get(i);
                Integer val=bridges.get(tmp.linkNum);
                if (val!=null && val==1) {
                    tmp.count--;
                    if (tmp.count == 0) {
                        ll.remove(i);
                        i--;
                    }
                    bridges.put(tmp.linkNum, 0);
                }
            }
        }

    }

    public void findAllBridges() {

        colour = new int[n];
        dfsNum = new int[n];
        num = 0;
        Arrays.fill(colour, 0);
        for (int i = 0; i < links.size(); i++) {
            if (links.get(i) != null && links.get(i).size() > 0) {
                int start = links.get(i).get(0).linkNum / n;
                dfs(start, -1, n);
            }
        }

    }

    int dfs(int u, int p, int n) {
        colour[u] = 1;
        dfsNum[u] = num++;
        int lastAncestor = num;
        for (int v = 0; v < n; v++) {
            if (graph[u][v] > 0 && v != p) {
                if (colour[v] == 0) {
                    int rec = dfs(v, u, n);
                    if (rec > dfsNum[u]) {
                        // System.out.println("Bridge found: " + u + " " + v);
                        if (!bridges.containsKey(convert(u, v))) {
                            bridges.put(convert(u, v), 1);
                        }
                    }
                    lastAncestor = Math.min(lastAncestor, rec);
                } else {
                    lastAncestor = Math.min(lastAncestor, dfsNum[v]);
                }
            }
        }
        colour[u] = 2;
        return lastAncestor;

    }

    public int convert(int i, int j) {
        int res = Math.min(i, j) * n + Math.max(i, j);
        return res;
    }

    public int convertRev(int i, int j) {
        int res = Math.max(i, j) * n + Math.min(i, j);
        return res;
    }
    void fillTree(int start, int numTree, ArrayList<link> ll) {
        visited[start] = true;
        for (int i = 0; i < n; i++) {
            if (graph[start][i] > 0 && !visited[i]) {
                int con = convert(start, i);
                ll.add(new link(con, graph[start][i]));
                passedLinks.put(con, 1);
                tree[i] = numTree;
                fillTree(i, numTree, ll);
            } else if (graph[start][i] > 0 && visited[i]
                    && !passedLinks.containsKey(convert(start, i))) {
                int con = convert(start, i);
                ll.add(new link(con, graph[start][i]));
                passedLinks.put(con, 1);
                tree[i] = numTree;
            }
        }
    }

    public void bfs() {
        numTrees = 0;
        visited = new boolean[n];

        while (true) {
            int start;
            for (start = 0; start < n; start++) {
                if (tree[start] == -1)
                    break;
            }
            if (start == n)
                break;
            tree[start] = numTrees;
            treeRoots[numTrees] = start;
            visited[start] = true;
            ArrayList<link> linksI = new ArrayList<link>();
            fillTree(start, numTrees, linksI);
            links.add(linksI);
            numTrees++;
        }

    }

    public int n;
    public int[] tree;
    public int[] treeRoots;
    public boolean[] visited;
    public int[][] graph;
    public int numTrees;
    public ArrayList<ArrayList<link>> links = new ArrayList<ArrayList<link>>();
    HashMap<Integer, Integer> passedLinks = new HashMap<Integer, Integer>();
    HashMap<Integer, Integer> bridges = new HashMap<Integer, Integer>();

    public void solve() {

        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        graph = new int[n][n];
        tree = new int[n];
        treeRoots = new int[n];

        Arrays.fill(tree, -1);

        for (int i = 0; i < n - 1; i++) {
            int s1 = read.nextInt() - 1;
            int s2 = read.nextInt() - 1;
            graph[s1][s2]++;
            graph[s2][s1]++;
        }
        // find all links and trees
        bfs();
        if (numTrees == 1) {
            System.out.println(0);
            return;
        }
        // System.out.println(Arrays.toString(tree));
        // System.out.println(links.size());
        // for (ArrayList<link> ll : links) {
        // System.out.println(Arrays.toString(ll.toArray()));
        // }
        clearBridges();

        // System.out.println("cleared");
        // for (ArrayList<link> ll : links) {
        // System.out.println(Arrays.toString(ll.toArray()));
        // }
        ArrayList<String> result = new ArrayList<String>();

        boolean exit = false;
        int count = 0;
        while (!exit) {
            for (int i = 0; i < links.size(); i++) {
                if (links.get(i) != null && links.get(i).size() > 0) {
                    for (int j = 0; j < links.size(); j++) {
                        if (j != i && links.get(j) != null) {
                            link lnk = links.get(i).get(0);
                            if (lnk.count == 1) {
                                lnk.count--;
                                links.get(i).remove(0);
                            } else {
                                lnk.count--;
                            }

                            //System.out.println(lnk.count);
                            result.add((lnk.linkNum / n + 1) + " "
                                    + (lnk.linkNum % n + 1) + " "
                                    + (treeRoots[i] + 1) + " "
                                    + (treeRoots[j] + 1));
                            graph[lnk.linkNum / n][lnk.linkNum % n]--;
                            graph[lnk.linkNum % n][lnk.linkNum / n]--;
                            graph[treeRoots[i]][treeRoots[j]]++;
                            graph[treeRoots[j]][treeRoots[i]]++;
                            int min = Math.min(i, j);
                            int max = Math.max(i, j);
                            links.get(min).addAll(links.get(max));
                            links.set(max, null);
                            count++;
                            clearBridges();
                            break;
                        }
                    }
                }
                // System.out.println(count + " " + numTrees);
                if (count == numTrees - 1) {
                    exit = true;
                    break;
                }
            }
        }

        System.out.println(count);
        for (int i = 0; i < count; i++) {
            System.out.println(result.get(i));
        }
    }

    public static void main(String[] args) {
        new Roads_not_only_in_Berland().solve();

    }

}