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;
const int maxn = 100000 + 5;
int indeg[maxn], fa[maxn], co[maxn], dfn[maxn], low[maxn], cs[maxn], rc[maxn],
    num, tco;
int sta[maxn], ins[maxn], stop;
int wzj[maxn];
vector<int> sagitta[maxn];
int n;
void dfs(int x) {
  dfn[x] = low[x] = ++num;
  sta[++stop] = x;
  ins[x] = true;
  int v = fa[x];
  if (!ins[v]) {
    dfs(v);
    low[x] = min(low[x], low[v]);
  } else {
    if (ins[v] == 1) low[x] = min(low[x], dfn[v]);
  }
  if (low[x] == dfn[x]) {
    while (sta[stop] != x) {
      co[sta[stop]] = tco;
      ins[sta[stop]] = 2;
      cs[tco]++;
      stop--;
    }
    if (cs[tco]) {
      cs[tco]++;
      co[sta[stop]] = tco++;
    }
    ins[sta[stop]] = 2;
    stop--;
  }
}
int dfs2(int x) { return rc[x] = (rc[x] ? rc[x] : dfs2(fa[x])); }
int main() {
  tco = 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> fa[i];
    indeg[fa[i]] = i;
  }
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) dfs(i);
  for (int i = 1; i <= n; i++) rc[i] = co[i];
  for (int i = 1; i <= n; i++) dfs2(i);
  int leaf = 0;
  for (int i = 1; i <= n; i++)
    if (indeg[i] == 0) {
      sagitta[rc[i]].push_back(i);
      leaf += (sagitta[rc[i]].size() != 1);
    }
  for (int i = 1; i <= n; i++)
    if (co[i]) wzj[co[i]] = i;
  tco--;
  if (tco == 1 && cs[1] == n) {
    puts("0");
    return 0;
  }
  printf("%d\n", tco + leaf);
  if (tco > 1) {
    for (int i = 1; i <= tco; i++) {
      int u = wzj[i];
      int v = sagitta[(i % tco) + 1].size() == 0 ? wzj[(i % tco) + 1]
                                                 : sagitta[(i % tco) + 1][0];
      printf("%d %d\n", u, v);
    }
  }
  for (int i = 1; i <= tco; i++)
    for (int j = (tco > 1); j < (int)sagitta[i].size(); j++)
      printf("%d %d\n", wzj[i], sagitta[i][j]);
  return 0;
}