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.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ProblemE {
	
	static final int INF = 1000000;
	
    static List<Integer> graph[];
    static List<Integer> r_graph[];
    static List<Integer> rev;
    static boolean[] visited;
    static int[] node_id;
    public static void init(int len) {
         graph = new List[len];
         r_graph = new List[len];
         for (int i = 0 ; i < len ; i++) {
              graph[i] = new ArrayList<Integer>();
              r_graph[i] = new ArrayList<Integer>();
         }
    }
   
    public static void add(int i, int j) {
         graph[i].add(j);
         r_graph[j].add(i);
    }
   
    public static void dfs(int i) {
         visited[i] = true;
         for (int next : graph[i]) {
              if (!visited[next]) {
                   dfs(next);
              }
         }
         rev.add(i);
    }
   
    public static void rdfs(int i, int id) {
         visited[i] = true;
         node_id[i] = id;
         for (int next : r_graph[i]) {
              if (!visited[next]) {
                   rdfs(next, id);
              }
         }
    }
   
    public static int scc(int len) {
         visited = new boolean[len];
         rev = new ArrayList<Integer>();
         for (int i = 0 ; i < len ; i++) {
              if (!visited[i]) {
                   dfs(i);
              }
         }
        
         int id = 0;
         node_id = new int[len];
         visited = new boolean[len];
         for (int i = rev.size() - 1 ; i >= 0 ; i--) {
              if (!visited[rev.get(i)]) {
                   rdfs(rev.get(i), id);
                   id++;
              }
         }
         return id;
    }


//有向グラフの頭を求める
        

         
	public static void main(String[] args) throws IOException {
		BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);

		int n = Integer.valueOf(s.readLine());
		String[] data = s.readLine().split(" ");
		
		init(n);
		for (int i = 0 ; i < n ; i++) {
			int t = Integer.valueOf(data[i]) - 1;
			add(i, t);
		}
		int len = scc(n);
		if (len == 1) {
			out.println(0);
			out.println();
			out.flush();
			return;
		}
		
		List<Integer>[] groups = new List[len];
		for (int i = 0 ; i < len ; i++) {
			groups[i] = new ArrayList<Integer>();
		}
		for (int i = 0 ; i < n ; i++) {
			groups[node_id[i]].add(i);
		}

		int cnt = 0;
		List<Integer> head = new ArrayList<Integer>();
		List<Integer> tail = new ArrayList<Integer>();
		Map<Integer, Integer> tailMap = new HashMap<Integer, Integer>();
		for (int i = 0 ; i < len ; i++) {
			boolean ishead = true;
			boolean istail = true;
			for (int v : groups[i]) {
				for (int from : r_graph[v]) {
					if (node_id[from] != i) {
						ishead = false;
					}
				}
				for (int to : graph[v]) {
					if (node_id[to] != i) {
						istail = false;
					}
				}
			}
			if (ishead) {
				head.add(i);
			}
			if (istail) {
				tailMap.put(i, tail.size());
				tail.add(i);
			}
		}

		// debug(node_id);
		// debug(head);
		// debug(tail);
		memo = new int[n];
		Arrays.fill(memo, -1);
		int hl = head.size();
		int tl = tail.size();
		List<Integer>[] tails = new List[tl];
		for (int i = 0 ; i < tl ; i++) {
			tails[i] = new ArrayList<Integer>();
		}
		for (int i = 0 ; i < hl ; i++) {
			int hi = groups[head.get(i)].get(0);
			int ti = tailMap.get(node_id[go(hi)]);
			tails[ti].add(hi);
		}
		
		
        out.println(hl);
        List<Integer> tr = new ArrayList<Integer>();
        List<Integer> hr = new ArrayList<Integer>();
        for (int i = 0 ; i < tl ; i++) {
        	int thl = tails[i].size();
        	int tid = groups[tail.get(i)].get(0) + 1;
        	for (int t = 1 ; t < thl ; t++) {
        		out.println(tid + " " + (tails[i].get(t) + 1));
        	}
        	if (thl >= 1) {
        		tr.add(tid);
        		hr.add(tails[i].get(0) + 1);
        	}
        }
        int l = tr.size();
        for (int i = 0 ; i < l ; i++) {
        	int t = tr.get(i);
        	int h = hr.get((i+1)%l);
        	out.println(t + " " + h);
        }
        
		out.flush();
	}
	
	static int[] memo;
	private static int go(int now) {
		if (memo[now] != -1) {
			return memo[now];
		}
		
		int next = graph[now].get(0);
		int ans = next;
		if (node_id[now] != node_id[next]) {
			ans = go(next);
		}
		memo[now] = ans;
		return ans;
	}

	public static void debug(Object... os){
		System.err.println(Arrays.deepToString(os));
	}
}