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>
using namespace std;
int n;
int A[200005];
vector<int> G[200005], GT[200005];
char vis[200005];
int L[200005], l;
int numer[200005], nr;
int przed[200005];
vector<int> T[200005];
int st[200005];
int E[200005], e;
int ok[200005];
vector<pair<int, int> > res;
inline void dfs1(int x) {
  vis[x] = 1;
  for (int i = 0; i < (((int)(G[x]).size())); i++)
    if (!vis[G[x][i]]) dfs1(G[x][i]);
  L[l++] = x;
}
inline void dfs2(int x) {
  vis[x] = 1;
  numer[x] = nr;
  for (int i = 0; i < (((int)(GT[x]).size())); i++)
    if (!vis[GT[x][i]]) dfs2(GT[x][i]);
}
vector<int> result;
inline bool dfs(int x) {
  vis[x] = 1;
  if (((int)(T[x]).size()) == 0) {
    result.push_back(x);
    return true;
  }
  bool f = false;
  for (int i = 0; i < (((int)(T[x]).size())); i++)
    if (!vis[T[x][i]]) {
      f |= dfs(T[x][i]);
    }
  return f;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n); i++) {
    scanf("%d", &A[i]), A[i]--;
    G[i].push_back(A[i]);
    GT[A[i]].push_back(i);
  }
  for (int i = 0; i < (n); i++)
    if (!vis[i]) dfs1(i);
  for (int i = 0; i < (n); i++) vis[i] = 0;
  nr = 0;
  for (int i = (n - 1); i >= (0); i--)
    if (!vis[L[i]]) {
      dfs2(L[i]);
      przed[nr] = L[i];
      nr++;
    }
  if (nr == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (((int)(G[i]).size())); j++)
      if (numer[i] != numer[G[i][j]]) T[numer[i]].push_back(numer[G[i][j]]);
  for (int i = 0; i < (nr); i++) {
    sort(T[i].begin(), T[i].end());
    int k = unique(T[i].begin(), T[i].end()) - T[i].begin();
    while (((int)(T[i]).size()) > k) T[i].pop_back();
  }
  for (int i = 0; i < (nr); i++)
    for (int j = 0; j < (((int)(T[i]).size())); j++) st[T[i][j]]++;
  vector<int> aa, bb;
  int bbb;
  for (int i = 0; i < (nr); i++) {
    if (!st[i]) E[e++] = i;
    if (((int)(T[i]).size()) == 0) bbb = i;
  }
  for (int i = 0; i < (n); i++) vis[i] = 0;
  for (int i = 0; i < (e); i++) {
    bool f = dfs(E[i]);
    if (f) {
      res.push_back(make_pair(result.back(), E[(i + 1) % e]));
      ok[result.back()] = 1;
      ok[E[(i + 1) % e]] = 1;
      if (((int)(result).size()) > 1) result.pop_back();
    } else {
      ok[res.back().second] = 0;
      res.back().second = E[(i + 1) % e];
      ok[E[(i + 1) % e]] = 1;
    }
  }
  for (int i = 0; i < (nr); i++) {
    if (!st[i] && !ok[i]) aa.push_back(i);
    if (((int)(T[i]).size()) == 0 && !ok[i]) bb.push_back(i);
  }
  if (((int)(aa).size()) || ((int)(bb).size())) {
    if (aa.empty()) aa.push_back(E[0]);
    if (bb.empty()) bb.push_back(bbb);
    for (int i = 0; i < (max(((int)(aa).size()), ((int)(bb).size()))); i++)
      res.push_back(make_pair(bb[min(i, ((int)(bb).size()) - 1)],
                              aa[min(i, ((int)(aa).size()) - 1)]));
  }
  printf("%d\n", ((int)(res).size()));
  for (int i = 0; i < (((int)(res).size())); i++)
    printf("%d %d\n", przed[res[i].first] + 1, przed[res[i].second] + 1);
  return 0;
}