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.io.*;
import java.util.*;

public class CF22E
{
	static final boolean _DEBUG = true;

	static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner(BufferedReader _br) {
			br = _br;
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					e.printStackTrace();
					return "";
				}
			}
			return st.nextToken();
		}

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

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

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

	static MyScanner   scan;
	static PrintWriter out;

	static int debugCount = 0;
	static void debug(String msg) {
		if (_DEBUG && debugCount < 200) {
			out.println(msg);
			out.flush();
			debugCount++;
		}
	}
	
    public static void main (String args[]) throws IOException {
//    	scan = new MyScanner(new BufferedReader(new FileReader("test.in")));
    	scan = new MyScanner(new BufferedReader(new InputStreamReader(System.in)));
    	out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        CF22E inst = new CF22E();
        inst.execute2();
        out.close();
    }
    
	int compLen = 0;
	Stack<Node> order = new Stack<Node>();
	
	class Node {
		int id;
		int compID = -1;
		boolean used = false;
		Node nb1;
		LinkedList<Node> nbs2 = new LinkedList<Node>();
		Node(int _id) {
			id = _id + 1;
		}
		
		void dfs1() {
			used = true;
			if (!nb1.used) {
				nb1.dfs1();
			}
			order.add(this);
		}
		
		void dfs2() {
			used = true;
			compID = compLen;
			for (Node nb : nbs2) {
				if (!nb.used) {
					nb.dfs2();
				}
			}			
		}
	}
	
    void execute() throws IOException {
    	int n = scan.nextInt();
    	Node[] nodes = new Node[n];
    	for (int i = 0; i < n; i++) {
    		nodes[i] = new Node(i);
    	}
    	for (Node node: nodes) {
    		Node next = nodes[scan.nextInt()-1];
    		node.nb1 = next;
    		next.nbs2.add(node);
    	}
    	
		for (Node node : nodes) {
			if (!node.used) {
				node.dfs1();
			}
		}
		for (Node node : nodes) {
			node.used = false;
		}
		ArrayList<Integer> leader = new ArrayList<Integer>();
		
		while (!order.isEmpty()) {
			Node node = order.pop();
			if (!node.used) {
				node.dfs2();
				leader.add(node.id);
				compLen++;
			}
		}
		
    	int[] indegree = new int[compLen];
    	int[] outdegree = new int[compLen];
    	for (Node node: nodes) {
    		if (node.compID != node.nb1.compID) {
    			indegree[node.nb1.compID]++;
    			outdegree[node.compID]++;
    		}
    	}
    	if (compLen == 1) {
    		out.println(0);
    		return;
    	}
    	ArrayList<Integer> inList = new ArrayList<Integer>();
    	ArrayList<Integer> outList = new ArrayList<Integer>();
    	for (int i = 0; i < compLen; i++) {
    		if (indegree[i] == 0) inList.add(i);
    		if (outdegree[i] == 0) outList.add(i);
    	}
    	
    	int min = Math.min(inList.size(), outList.size());
    	int max = Math.max(inList.size(), outList.size());
    	out.println(max);
    	for (int i = 0; i < min; i++) {
    		out.println(leader.get(outList.get(i)) + " " + leader.get(inList.get(i)));
    	}
    	for (int i = min; i < max; i++) {
    		if (inList.size() < outList.size()) {
        		out.println(leader.get(outList.get(i)) + " " + leader.get(inList.get(inList.size()-1)));    			
    		} else {
        		out.println(leader.get(outList.get(outList.size()-1)) + " " + leader.get(inList.get(i)));    			
    		}
    	}
    }
    
    int n;
    int[] next;
    int[] count;
    boolean[] visited;
    int dfs(int root) {
    	visited[root] = true;
    	int a = next[root];
    	if (visited[a]) {
    		return a;
    	} else {
    		return dfs(a);
    	}
    }
    
    void execute2() throws IOException {
    	n = scan.nextInt();
    	next = new int[n];
    	count = new int[n];
    	for (int i = 0; i < n; i++) {
    		int c = scan.nextInt()-1;
    		next[i] = c;
    		count[c]++;
    	}
    	visited = new boolean[n];
    	ArrayList<Integer> starts = new ArrayList<Integer>();
    	ArrayList<Integer> ends = new ArrayList<Integer>();
    	for (int i = 0; i < n; i++) {
    		if (count[i] == 0) {
    			starts.add(i);
    			ends.add(dfs(i));
    		}
    	}
    	for (int i = 0; i < n; i++) {
    		if (!visited[i]) {
    			starts.add(i);
    			ends.add(dfs(i));
    		}
    	}
    	int k = starts.size();
    	if (k == 1 && starts.get(0) == ends.get(0)) {
    		out.println(0);
    		return;
    	}
    	
    	out.println(k);
    	for (int i = 0; i < k; i++) {
    		out.println((ends.get(i)+1) + " " + (starts.get((i+1)%k)+1));
    	}
    }
}