Problem: To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?

Input

The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.

Output

In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.

Examples

Input

3
3 3 2


Output

1
3 1


Input

7
2 3 1 3 4 4 1


Output

3
2 5
2 6
3 7

Solution (in JAVA):

Solution:
import java.util.*;
import java.io.*;

public class E0022 {

    public static void main(String args[]) throws Exception {
        new E0022();
    }

    E0022() throws Exception {
        PandaScanner sc = null;
        PrintWriter out = null;
        try {
            sc = new PandaScanner(System.in);
            out = new PrintWriter(System.out);
        } catch (Exception ignored) {
        }

        int n = sc.nextInt();
        int to[] = new int[n];

        Graph g = new Graph(n);
        for (int i = 0; i < n; i++) {
            to[i] = sc.nextInt() - 1;
            g.add(i, to[i]);
        }

        SCC s = new SCC(g);
        if (s.cnt == 1) {
            out.println("0");
        }
        else {
            int c = s.cnt;
            ArrayList<Integer> owns[] = new ArrayList[c];
            ArrayList<Integer> adj[] = new ArrayList[c];
            for (int i = 0; i < c; i++) {
                owns[i] = new ArrayList<Integer>();
                adj[i] = new ArrayList<Integer>();
            }
            int inDegree[] = new int[c];
            int outDegree[] = new int[c];

//            System.out.println(Arrays.toString(s.id));

            for (int i = 0; i < n; i++) {
                int uid = s.id[i];
                int vid = s.id[to[i]];
                owns[uid].add(i + 1);

                if (uid != vid) {
                    outDegree[uid]++;
                    inDegree[vid]++;
                    adj[vid].add(uid);
                }
            }

//            System.out.println(Arrays.toString(outDegree));
//            System.out.println(Arrays.toString(inDegree));

            ArrayList<ArrayList<Integer>> ins = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> outs = new ArrayList<Integer>();

            int edgesLeft = 0;

            //Find the ends of the paths and match it with the out of the paths
            for (int i = 0; i < c; i++) {
                if (outDegree[i] > 1) {
                    throw new RuntimeException("Out degree of node too large...?");
                }
                if (outDegree[i] == 0) {
                    ArrayList<Integer> lst = new ArrayList<Integer>();

                    ArrayDeque<Integer> q = new ArrayDeque<Integer>();
                    q.add(i);
                    while (!q.isEmpty()) {
                        int u = q.poll();
                        if (adj[u].isEmpty()) {
                            edgesLeft++;
                            lst.add(u);
                        }
                        else {
                            for (Integer v: adj[u]) {
                                q.add(v);
                            }
                        }
                    }

                    ins.add(lst);
                    outs.add(i);
                }
            }

            //Print the list
            out.println(edgesLeft);
            for (int i = 0; i < outs.size(); i++) {
                int uComp = outs.get(i);
                int u = owns[uComp].get(0);
                ArrayList<Integer> lst = ins.get((i + 1) % outs.size());
                for (Integer vComp: lst) {
                    out.println(u + " " + owns[vComp].get(0));
                }
            }
        }


        out.close();
        System.exit(0);
    }

    public class Graph {
        final int INF = 1000000000;
        int N;
        ArrayList<Integer> e[];

        public Graph(int nn) {
            e = new ArrayList[N = nn];
            for (int i = 0; i < N; e[i++] = new ArrayList<Integer>());
        }

        void add(int u, int v) {
            e[u].add(v);
        }
    }

    public class SCC {
        Graph g;
        int v, cnt, pre, low[], id[];
        boolean visited[];
        Stack<Integer> stk = new Stack<Integer>();
        HashSet<Integer> set = new HashSet<Integer>();

        public SCC(Graph gg) {
            g = gg;
            low = new int[v = g.N];
            id = new int[v];
            cnt = pre = 0;
            visited = new boolean[v];

            for (int i = 0; i < v; dfs(i++));
        }
        void dfs(int i) {
            if (visited[i]) return;
            visited[i] = true;
            int currPre = (low[i] = pre++);
            stk.push(i);
            set.add(i);
            for (Integer j: g.e[i]) {
                dfs(j);
                if (set.contains(j) && low[j] < low[i])
                    low[i] = low[j];
            }

            if (currPre == low[i]) {
                while(!stk.isEmpty() && stk.peek() != i) {
                    int j = stk.pop();
                    id[j] = cnt;
                    set.remove(j);
                }
                id[stk.pop()] = cnt++;
                set.remove(i);
            }
        }
    }

    //The PandaScanner class, for Panda fast scanning!
    public class PandaScanner {
        BufferedReader br;
        StringTokenizer st;
        InputStream in;

        PandaScanner(InputStream in) throws Exception {
            br = new BufferedReader(new InputStreamReader(this.in = in));
        }

        public String next() throws Exception {
            if (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine().trim());
                return next();
            }
            return st.nextToken();
        }

        public boolean hasNext() throws Exception {
            return (st != null && st.hasMoreTokens()) || in.available() > 0;
        }

        public long nextLong() throws Exception {
            return Long.parseLong(next());
        }

        public int nextInt() throws Exception {
            return Integer.parseInt(next());
        }
    }
}