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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;


public class scheme {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		Tarjan ta=new Tarjan(n);
		for(int i=0;i<n;i++) {
			int x=scan.nextInt()-1;
			ta.a.add(i,x);
		}
		ta.go();
		int[] rep=new int[ta.scc];
		Arrays.fill(rep,-1);
		Graph meta=new Graph(ta.scc);
		Graph rev=new Graph(ta.scc);
		int[] in=new int[ta.scc], out=new int[ta.scc];
		for(int i=0;i<n;i++) {
			rep[ta.id[i]]=i;
			for(int nxt:ta.a.a[i]) {
				if(ta.id[i]!=ta.id[nxt]) {
					meta.add(ta.id[i],ta.id[nxt]);
					in[ta.id[nxt]]++;
					out[ta.id[i]]++;
					rev.add(ta.id[nxt],ta.id[i]);
				}
			}
		}
		if(ta.scc==1) {
			System.out.println(0);
		}
		else {
			ArrayList<Integer> start=new ArrayList<>(), finish=new ArrayList<>();
			ArrayDeque<Integer> q=new ArrayDeque<>();
			for(int i=0;i<ta.scc;i++) {
				if(out[i]==0) {
					ArrayList<Integer> degree0=new ArrayList<>();
					q.offer(i);
					while(!q.isEmpty()) {
						int cur=q.poll();
						for(int nxt:rev.a[cur]) {
							q.offer(nxt);
							if(in[nxt]==0) degree0.add(rep[nxt]+1);
						}
					}
					for(int j=0;j<degree0.size();j++) {
						start.add(degree0.get(j));
						finish.add(rep[i]+1);
					}
					if(degree0.size()==0) {
						start.add(rep[i]+1);
						finish.add(rep[i]+1);
					}
				}
			}
			System.out.println(start.size());
			for(int i=0;i<start.size();i++) {
				int nxt=(i+1)%start.size();
				System.out.println(finish.get(i)+" "+start.get(nxt));
			}
		}
	}
	static class Graph {
		ArrayList<Integer>[] a;
		int n;
		Graph(int n) {
			this.n=n;
			a=new ArrayList[n];
			for(int i=0;i<n;i++) a[i]=new ArrayList<>();
		}
		void add(int u, int v) {
			a[u].add(v);
		}
	}
	static class Tarjan {
		int[] id,pre,lo;
		int n,scc,ct;
		ArrayDeque<Integer> q;
		Graph a;
		Tarjan(int n) {
			this.n=n;
			scc=0;
			ct=0;
			id=new int[n];
			pre=new int[n];
			lo=new int[n];
			Arrays.fill(pre,-1);
			a=new Graph(n);
			q=new ArrayDeque<>();
		}
		void addEdge(int u, int v) {
			a.a[u].add(v);
		}
		void go() {
			for(int i=0;i<n;i++) {
				if(pre[i]==-1) dfs(i);
			}
		}
		void dfs(int at) {
			pre[at]=lo[at]=ct++;
			q.offer(at);
			for(int nxt:a.a[at]) {
				//fwd edge
				if(pre[nxt]==-1) {
					dfs(nxt);
				}
				lo[at]=Math.min(lo[at],lo[nxt]);
			}
			//root of scc
			if(lo[at]==pre[at]) {
				int x=-1;
				while(x!=at) {
					x=q.pollLast();
					id[x]=scc;
					lo[x]=n;
				}
				scc++;
			}
		}
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner() {
			try	{
				br = new BufferedReader(new InputStreamReader(System.in));
				st = new StringTokenizer(br.readLine());
			} catch (Exception e){e.printStackTrace();}
		}

		public String next() {
			if (st.hasMoreTokens())	return st.nextToken();
			try {st = new StringTokenizer(br.readLine());}
			catch (Exception e) {e.printStackTrace();}
			return st.nextToken();
		}

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

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

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

		public String nextLine() {
			String line = "";
			if(st.hasMoreTokens()) line = st.nextToken();
			else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
			while(st.hasMoreTokens()) line += " "+st.nextToken();
			return line;
		}
	}
}
/*
8
2 3 4 2 6 7 8 6
*/