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;
vector<vector<long long>> v;
long long dfn[100020];
long long lw[100020];
long long timer = 1;
bool instack[100020];
stack<long long> st;
vector<vector<long long>> cmps;
vector<vector<long long>> vv;
long long cmp[100020];
void dfs(long long node) {
  lw[node] = dfn[node] = timer++;
  st.push(node);
  instack[node] = 1;
  for (auto child : v[node]) {
    if (dfn[child] == -1) {
      dfs(child);
      lw[node] = min(lw[child], lw[node]);
    } else if (instack[child])
      lw[node] = min(lw[child], lw[node]);
  }
  if (lw[node] == dfn[node]) {
    long long x = -1;
    cmps.push_back(vector<long long>());
    while (x != node) {
      x = st.top();
      st.pop();
      instack[x] = 0;
      cmps.back().push_back(x);
      cmp[x] = cmps.size() - 1;
    }
  }
}
bool in[100020];
vector<pair<long long, long long>> ans;
vector<vector<long long>> des;
vector<vector<long long>> src;
long long par[100020];
long long find(long long node) {
  while (node != par[node]) {
    par[node] = par[par[node]];
    node = par[node];
  }
  return node;
}
void un(long long n1, long long n2) {
  long long x = find(n1);
  long long y = find(n2);
  par[x] = y;
}
bool out[100020];
map<long long, bool> tk;
vector<long long> prs;
long long id[100020];
void add(long long c1, long long c2) {
  for (long long i = 0, j = 0; i != des[c1].size() || j != src[c2].size();) {
    i = min(i, (long long)des[c1].size() - 1);
    j = min(j, (long long)src[c2].size() - 1);
    long long n1 = des[c1][i];
    long long n2 = src[c2][j];
    ans.push_back({cmps[n1][0], cmps[n2][0]});
    i++, j++;
  }
}
bool vis[100020];
void dfs1(long long node) {
  vis[node] = 1;
  for (auto child : vv[node]) {
    un(node, child);
    if (!vis[child]) dfs1(child);
  }
}
void wr(vector<vector<long long>> t, long long n) {
  for (long long i = 0; i < n; i++) {
    cout << i << ":\n";
    for (auto it : t[i]) cout << it << " ";
    cout << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  (v) = vector<vector<long long>>((n) + 2);
  memset(dfn, -1, sizeof dfn);
  for (long long i = 1; i <= n; i++) {
    long long k;
    cin >> k;
    v[i].push_back(k);
  }
  for (long long i = 1; i <= n; i++)
    if (dfn[i] == -1) dfs(i);
  (vv) = vector<vector<long long>>((n) + 2);
  for (long long i = 0; i <= n; i++) par[i] = i;
  for (long long i = 1; i <= n; i++) {
    for (auto child : v[i]) {
      if (cmp[child] != cmp[i]) {
        vv[cmp[i]].push_back(cmp[child]);
        in[cmp[child]] = 1;
        out[cmp[i]] = 1;
      }
    }
  }
  long long cnt = cmps.size();
  for (long long i = 0; i < cnt; i++) {
    if (!vis[i]) dfs1(i);
  }
  if (cnt == 1) return cout << 0, 0;
  for (long long i = 0; i < cnt; i++) {
    if (!tk[find(i)]) {
      tk[find(i)] = 1;
      prs.push_back(find(i));
      id[find(i)] = prs.size() - 1;
    }
  }
  (des) = vector<vector<long long>>((n) + 2);
  (src) = vector<vector<long long>>((n) + 2);
  for (long long i = 0; i < cnt; i++) {
    if (in[i] == 0) {
      src[id[find(i)]].push_back(i);
    }
    if (out[i] == 0) des[id[find(i)]].push_back(i);
  }
  for (long long i = 0; i < prs.size(); i++) {
    add(i, (i + 1) % prs.size());
  }
  cout << ans.size();
  cout << '\n';
  for (auto p : ans) cout << p.first << " " << p.second << '\n';
  return 0;
}