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.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;
public class cf22e {
  public static void main(String[] args) {
    FastScanner in = new FastScanner(System.in);
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    int n = in.nextInt();
    ArrayList<Integer>[] g = new ArrayList[n];
    for(int i=0; i<n; i++) 
      g[i] = new ArrayList<Integer>();
    for(int i=0; i<n; i++) 
      g[i].add(in.nextInt()-1);
    DisjointSet ds = new DisjointSet(n);
    SCC scc = new SCC(g);
    scc.run();
    if(scc.count == 1) {
      System.out.println(0);
      return;
    }
    int[] ids = scc.id;
    int[] indeg = new int[scc.count];
    int[] outdeg = new int[scc.count];
    int[] rep = new int[scc.count];
    for(int i=0; i<n; i++) {
      rep[ids[i]] = i;
      for(int j : g[i]) {
        ds.union(ids[i],ids[j]);
        if(ids[i] != ids[j]) {
          outdeg[ids[i]]++;
          indeg[ids[j]]++;
        }
      }
    }
    ArrayList<Integer>[] outs = new ArrayList[n];
    ArrayList<Integer>[] ins = new ArrayList[n];
    for(int i=0; i<n; i++) {
      outs[i] = new ArrayList<Integer>();
      ins[i] = new ArrayList<Integer>();
    }
    for(int i=0; i<scc.count; i++) {
      if(indeg[i] == 0) ins[ds.find(i)].add(i);
      if(outdeg[i] == 0) outs[ds.find(i)].add(i);
    }
    ArrayList<ArrayList<Integer>> inlist = new ArrayList<ArrayList<Integer>>();
    ArrayList<ArrayList<Integer>> outlist = new ArrayList<ArrayList<Integer>>();
    for(ArrayList<Integer> x : ins) if(x.size() > 0) inlist.add(x);
    for(ArrayList<Integer> x : outs) if(x.size() > 0) outlist.add(x);
    ArrayList<Integer> ans = new ArrayList<Integer>();
    for(int i=0; i<inlist.size(); i++) {
      ans.add(rep[outlist.get(i).get(0)]);
      ans.add(rep[inlist.get((i+1)%inlist.size()).get(0)]);

      for(int j=1; j<inlist.get(i).size(); j++) {
        ans.add(rep[outlist.get(i).get(0)]);
        ans.add(rep[inlist.get(i).get(j)]);
      }

      for(int j=1; j<outlist.get(i).size(); j++) {
        ans.add(rep[outlist.get(i).get(j)]);
        ans.add(rep[inlist.get(i).get(0)]);
      }
    }
    out.println(ans.size()/2);
    for(int i=0; i<ans.size(); i+=2)
      out.println((ans.get(i)+1) + " " + (ans.get(i+1)+1));
    out.close();
  }
  static class DisjointSet {
    int[] p, r;
    public DisjointSet(int s) {
      p = new int[s];
      r = new int[s];
      for(int i=0; i<s; i++)
        p[i] = i;
    }
    public void union(int x, int y) {
      int a = find(x);
      int b = find(y);
      if(a==b) return;
      if(r[a] == r[b])
        r[p[b]=a]++;
      else 
        p[a]=p[b]=r[a]<r[b]?b:a;
    }
    public int find(int x) {
      return p[x]=p[x]==x?x:find(p[x]);
    }
  }
  static class SCC {
    int[] low, stk;
    int pre, n;
    public int count;
    public int[] id;
    ArrayList<Integer>[] g;

    @SuppressWarnings("unchecked")
    public SCC(int N) {
      low = new int[n = N];
      stk = new int[n + 1];
      Arrays.fill(id = new int[n], n);
      g = new ArrayList[n];
      for (int i = 0; i < n; i++)
        g[i] = new ArrayList<Integer>();
    }
    
    public SCC(ArrayList<Integer>[] G) {
      this(G.length);
      g=G;
    }

    public void add(int i, int j) {
      g[i].add(j);
    }

    public void run() {
      for (int i = 0; i < n; i++)
        if (id[i] == n)
          dfs(i);
    }

    // consider making the graph an arraydeque and popping as you go
    void dfs(int i) {
      Stack<int[]> trail = new Stack<int[]>();
      Stack<Integer> location = new Stack<Integer>();
      enter(i);
      location.push(i);
      if (first(i) != null)
        trail.push(first(i));
      while (!trail.isEmpty()) {
        int r = take(trail.peek());
        enter(r);
        location.push(r);
        int[] next = first(r);

        while (next == null && !trail.isEmpty()) {
          int[] top = trail.pop();
          exit(location.pop());
          undo(top);
          next = next(top);
        }

        if (next != null)
          trail.push(next);
      }
      exit(i);
    }

    int[] first(int i) {
      return i < 0 || g[i].size() == 0 ? null : new int[] { i, g[i].size() - 1 };
    }

    int[] next(int[] v) {
      return v[1] == 0 ? null : new int[] { v[0], v[1] - 1 };
    }

    int take(int[] v) {
      int i = v[0], jj = v[1];
      int j = g[i].get(jj);
      if (id[j] == n)
        return j;
      return -1;
    }

    void undo(int[] v) {
      int i = v[0], jj = v[1];
      int j = g[i].get(jj);
      if (low[j] < id[i])
        id[i] = low[j];
    }

    void enter(int i) {
      if (i >= 0)
        id[i] = low[stk[++stk[0]] = i] = pre++;
    }

    void exit(int i) {
      if (i < 0)
        return;
      if (id[i] < low[i])
        low[i] = id[i];
      else for (int j = i + ++count; j != i; id[j] = count - 1, low[j] = n)
        j = stk[stk[0]--];
    }
  }
  
  static class FastScanner {
    BufferedReader br;
    StringTokenizer st;
    
    public FastScanner(InputStream in) {
      br = new BufferedReader(new InputStreamReader(in));
      scanLine();
    }
    public void scanLine() {
      try {
        st = new StringTokenizer(br.readLine().trim());
      } catch(Exception e) {
        throw new RuntimeException(e.getMessage());
      }
    }
    public int numTokens() {
      if(!st.hasMoreTokens()) {
        scanLine();
        return numTokens();
      }
      return st.countTokens();
    }
    public String next() {
      if(!st.hasMoreTokens()) {
        scanLine();
        return next();
      }
      return st.nextToken();
    }
    public double nextDouble() {
      return Double.parseDouble(next());
    }
    public long nextLong() {
      return Long.parseLong(next());
    }
    public int nextInt() {
      return Integer.parseInt(next());
    }
  }
}