#include <bits/stdc++.h>
using namespace std;
struct comp {
  vector<int> v, l, r;
};
vector<comp> g;
int use[100001], in[100001], fr[100001];
vector<int> arr[100001];
void dfs(int v, comp& c) {
  if (use[v]) return;
  use[v] = 1;
  c.v.push_back(v);
  if (in[v] == 0) c.r.push_back(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, vector<int>& order) {
  if (use[v] == 1) return;
  use[v] = 1;
  topsort(gn[v], order);
  order.push_back(v);
}
void dfs2(int v, vector<int>& co) {
  if (use[v] == 1) return;
  use[v] = 1;
  co.push_back(v);
  for (int i = 0; i < gt[v].size(); i++) dfs2(gt[v][i], co);
}
int main() {
  vector<pair<int, int> > ans;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int v2;
    cin >> 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) {
      comp t;
      g.push_back(t);
      dfs(i, g.back());
    }
  }
  for (int j = 0; j <= n; j++) use[j] = 0;
  for (int i = 0; i < g.size(); i++) {
    vector<int> tsort, co;
    for (int j = 0; j < g[i].v.size(); j++) {
      if (!use[g[i].v[j]]) topsort(g[i].v[j], tsort);
    }
    for (int j = 0; j < tsort.size(); j++) use[tsort[j]] = 0;
    for (int j = tsort.size() - 1; j >= 0; j--) {
      if (co.size() <= 1)
        co.clear();
      else
        continue;
      if (!use[tsort[j]]) {
        dfs2(tsort[j], co);
      }
    }
    if (co.size() > 1) g[i].l = co;
  }
  if (g.size() == 1 && g[0].r.size() == 0) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < g.size(); i++) {
    int v1, v2;
    if (g[i].l.size())
      v1 = g[i].l.front();
    else
      v1 = g[i].v.front();
    int ii = (i + 1) % g.size();
    if (g[ii].r.size()) {
      v2 = g[ii].r.back();
      g[ii].r.pop_back();
    } else
      v2 = g[ii].v.front();
    ans.push_back(make_pair(v1, v2));
  }
  for (int i = 0; i < g.size(); i++)
    for (int j = 0; j < g[i].r.size(); j++) {
      ans.push_back(make_pair(g[i].l.back(), g[i].r[j]));
    }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}
