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 CPP):

Solution:
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
class TaskSolver {
 private:
  int timer, n, idC;
  vector<bool> used;
  vector<vector<int> > gt, cn;
  vector<int> g, fout, ssc, cid, dg, leader;

 public:
  void Init() {}
  bool Read() {
    if (scanf("%d", &n) != 1) return false;
    g.resize(n);
    gt.resize(n);
    cn.resize(n);
    fout.resize(n);
    dg.resize(n, 0);
    leader.resize(n);
    for (int i = 0; i < n; ++i) {
      int u;
      scanf("%d", &u);
      g[i] = u - 1;
      dg[u - 1]++;
      gt[u - 1].push_back(i);
    }
    return true;
  }
  void dfs1(int v) {
    timer++;
    used[v] = true;
    if (used[g[v]] == false) {
      dfs1(g[v]);
    }
    fout[v] = timer++;
  }
  void dfs2(int v) {
    used[v] = true;
    ssc.push_back(v);
    for (int i = 0; i < gt[v].size(); ++i) {
      int to = gt[v][i];
      if (used[to] == false) {
        dfs2(to);
      }
    }
  }
  void Solve() {
    idC = 0;
    timer = 0;
    cid.resize(n, -1);
    used.resize(n, false);
    for (int i = 0; i < n; ++i)
      if (used[i] == false) dfs1(i);
    int ans = 0;
    vector<pair<int, int> > ordering;
    for (int i = 0; i < n; ++i) {
      used[i] = false;
      ans += (dg[i] == 0);
      ordering.push_back(make_pair(fout[i], i));
    }
    for (int i = 0; i < n; ++i) {
      ;
      if (i == n - 1) {
        ;
      }
    }
    sort(ordering.begin(), ordering.end());
    reverse(ordering.begin(), ordering.end());
    for (int i = 0; i < n; ++i) {
      int from = ordering[i].second;
      if (used[from] == false) {
        ssc.clear();
        dfs2(from);
        if (ssc.size() > 1) {
          for (int j = 0; j < ssc.size(); ++j) {
            cid[ssc[j]] = idC;
            leader[idC] = ssc[j];
          }
          idC++;
        }
      }
    }
    for (int i = 0; i < n; ++i)
      ;
    for (int i = 0; i < n; ++i) {
      if (dg[i] == 0) {
        int starter = i;
        while (cid[starter] == -1) {
          starter = g[starter];
        }
        cn[leader[cid[starter]]].push_back(i);
      }
    }
    vector<pair<int, int> > edges;
    vector<pair<int, int> > w;
    set<int> took;
    for (int i = 0; i < n; ++i) {
      if (cn[i].size() == 1) {
        w.push_back(make_pair(cn[i][0], i));
        took.insert(i);
      } else if (cn[i].size() == 0) {
      } else {
        for (int j = 1; j < cn[i].size(); ++j) {
          edges.push_back(make_pair(i, cn[i][j]));
        }
        w.push_back(make_pair(cn[i][0], i));
        took.insert(i);
      }
    }
    for (int i = 0; i < n; ++i) {
      if (cid[i] != -1) {
        if (took.find(leader[cid[i]]) == took.end()) {
          took.insert(leader[cid[i]]);
          w.push_back(make_pair(leader[cid[i]], leader[cid[i]]));
        }
      }
    }
    for (int i = 0; i < w.size(); ++i) {
      int curr = i, next = (i + 1) % w.size();
      edges.push_back(make_pair(w[curr].second, w[next].first));
    }
    if (w.size() == 1) {
      if (w[0].first == w[0].second) {
        puts("0");
        return;
      }
    }
    printf("%d\n", edges.size());
    for (auto it = edges.begin(); it != edges.end(); ++it)
      printf("%d %d\n", it->first + 1, it->second + 1);
  }
};
int main() {
  for (;;) {
    TaskSolver taskSolver;
    taskSolver.Init();
    if (taskSolver.Read() == false) break;
    taskSolver.Solve();
  }
  return 0;
}