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 Main
{
	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)));
        Main inst = new Main();
        inst.execute();
        out.close();
    }
    
    class DisjointSet {
		int[] parent;
		int[] sizes;
		
		public DisjointSet(int _size) {
			parent = new int[_size];
			sizes = new int[_size];
			for (int i = 0; i < _size; i++) {
				parent[i] = i;
				sizes[i] = 1;
			}
		}
		
		int find(int x) {
			if (parent[x] == x) return x;
			parent[x] = find(parent[x]);
			return parent[x];
		}
		
		int size(int x) {
			return sizes[find(x)];
		}
		
		boolean union(int x, int y) {
			int rx = find(x);
			int ry = find(y);
			if (rx != ry) {
				parent[ry] = rx;
				sizes[rx] += sizes[ry];
				return true;
			} else {
				return false;
			}
		}			
	}
    
    class IntDisComp implements Comparator<Integer> {
		public int compare(Integer o1, Integer o2) {
			return dis.find(o1) - dis.find(o2);
		}
    	
    }
    
    int N, comp;
    int[] comps, childs;
    boolean[] checked;
    ArrayList<Boolean> compChecked;
    ArrayList<Integer> reps, indegree, tops, bottoms, compChilds;
    ArrayList<Integer>[] parents;
    ArrayList<int[]> added;
    Stack<Integer> stack;
    DisjointSet dis;
    
    void dfs1(int node) {
    	if (checked[node]) {
    		return;
    	}
    	checked[node] = true;
    	int child = childs[node];
    	if (!checked[child]) {
    		dfs1(child);
    	}
    	stack.add(node);
    }
    
    void dfs2(int node) {
    	if (comps[node] >= 0) {
    		return;
    	}
    	comps[node] = comp;
    	for (int parent : parents[node]) {
    		if (comps[parent] == -1) {
    			dfs2(parent);
    		} else if (dis.union(comps[node], comps[parent])) {
    			indegree.set(comps[node], indegree.get(comps[node])+1);
    			compChilds.set(comps[parent], comps[node]);
    		}
    	}
    }
    
    private void execute() throws IOException {
    	N = scan.nextInt();
    	comp = 0;
    	comps = new int[N];
    	childs = new int[N];
    	checked = new boolean[N];
    	compChecked = new ArrayList<Boolean>();
    	reps = new ArrayList<Integer>();
    	indegree = new ArrayList<Integer>();
    	tops = new ArrayList<Integer>();
    	bottoms = new ArrayList<Integer>();
    	parents = new ArrayList[N];
    	added = new ArrayList<int[]>();
    	compChilds = new ArrayList<Integer>();
    	stack = new Stack<Integer>();
    	dis = new DisjointSet(N);
    	
    	for (int i = 0; i < N; i++) {
    		comps[i] = -1;
    		parents[i] = new ArrayList<Integer>();
    	}
    	for (int i = 0; i < N; i++) {
    		childs[i] = scan.nextInt()-1;
    		parents[childs[i]].add(i);
    	}
    	for (int i = 0; i < N; i++) {
    		if (!checked[i]) {
    			dfs1(i);
    		}
    	}
    	while (!stack.isEmpty()) {
    		int i = stack.pop();
    		if (comps[i] == -1) {
    			reps.add(i);
    			indegree.add(0);
    			compChecked.add(false);
    			compChilds.add(-1);
    			dfs2(i);
    			comp++;
    		}
    	}
    	if (comp == 1) {
    		out.println("0");
    		return;
    	}
    	for (int i = 0; i < comp; i++) {
    		if (indegree.get(i) == 0) {
    			tops.add(reps.get(i));
    			bottoms.add(reps.get(dis.find(i)));
    		}
    	}
//    	Collections.sort(tops, new IntDisComp());
//    	Collections.sort(bottoms, new IntDisComp());
//    	System.out.println(Arrays.toString(comps));
//    	System.out.println(indegree);
//    	System.out.println(compChilds);
//    	System.out.println(tops);
//    	System.out.println(bottoms);
    	out.println(tops.size());
    	for (int i = 0; i < tops.size(); i++) {
    		int j = (i+1)%tops.size();
    		out.println((bottoms.get(i)+1)+" "+(tops.get(j)+1));
    	}
    }
}