#include <bits/stdc++.h>
using namespace std;
struct comp {
  vector<int> v;
  int l, r;
};
const int maxn = 100001;
comp g[maxn];
int r[maxn];
int rs;
int use[maxn], in[maxn], fr[maxn];
vector<int> arr[maxn];
void dfs(int v, comp& c) {
  if (use[v]) return;
  use[v] = 1;
  c.v.push_back(v);
  if (in[v] == 0) {
    if (c.r == 0)
      c.r = v;
    else
      r[rs++] = v;
  }
  for (int i = 0; i < arr[v].size(); i++) dfs(arr[v][i], c);
}
int gn[100001];
vector<int> gt[100001];
void topsort(int v, int* order, int& s1) {
  if (use[v] == 1) return;
  use[v] = 1;
  topsort(gn[v], order, s1);
  order[s1++] = v;
}
void dfs2(int v, int* co, int& s2) {
  if (use[v] == 1) return;
  use[v] = 1;
  co[s2++] = v;
  for (int i = 0; i < gt[v].size(); i++) dfs2(gt[v][i], co, s2);
}
pair<int, int> ans[100001];
int main() {
  int anss = 0;
  int n, s = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int v2;
    scanf("%d", &v2);
    fr[i]++;
    in[v2]++;
    arr[i].push_back(v2);
    arr[v2].push_back(i);
    gn[i] = v2;
    gt[v2].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (use[i] == 0) {
      dfs(i, g[s++]);
    }
  }
  int tsort[maxn], co[maxn], s1, s2;
  for (int j = 0; j <= n; j++) use[j] = 0;
  for (int i = 0; i < s; i++) {
    s1 = s2 = 0;
    for (int j = 0; j < g[i].v.size(); j++) {
      if (!use[g[i].v[j]]) topsort(g[i].v[j], tsort, s1);
    }
    for (int i = 0; i < s1; i++) use[tsort[i]] = 0;
    for (int j = s1 - 1; j >= 0; j--) {
      if (s2 <= 1)
        s2 = 0;
      else
        continue;
      if (!use[tsort[j]]) {
        dfs2(tsort[j], co, s2);
      }
    }
    if (s2 > 1) g[i].l = co[0];
  }
  if (s == 1 && g[0].r == 0) {
    cout << 0;
    return 0;
  }
  int ml;
  for (int i = 0; i < s; i++) {
    int v1, v2;
    v1 = g[i].l;
    ml = g[i].l;
    int ii = (i + 1) % s;
    if (g[ii].r) {
      v2 = g[ii].r;
    } else
      v2 = g[ii].v.back();
    ans[anss++] = make_pair(v1, v2);
  }
  for (int i = 0; i < rs; i++) ans[anss++] = make_pair(ml, r[i]);
  cout << anss << endl;
  for (int i = 0; i < anss; i++) printf("%d %d\n", ans[i].first, ans[i].second);
  return 0;
}
