#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
const int N = 1e5 + 2;
int n, low[N], num[N], ntar, ncc, cc[N];
vector<int> g[N], st, scc[N], gr[N];
bool in[N], indeg[N];
void dfs(int u) {
  num[u] = low[u] = ++ntar;
  in[u] = 1;
  st.emplace_back(u);
  for (int v : g[u]) {
    if (!num[v]) dfs(v);
    if (in[v]) low[u] = min(low[u], low[v]);
  }
  if (low[u] == num[u]) {
    ncc++;
    int v = 0;
    do {
      v = st.back();
      st.pop_back();
      in[v] = 0;
      cc[v] = ncc;
      scc[ncc].emplace_back(v);
    } while (u != v);
  }
}
vector<int> ed, uned, unst;
int dfs1(int u) {
  in[u] = 1;
  if (((int)(gr[u]).size()) == 0) return u;
  for (int v : gr[u])
    if (!in[v]) {
      int x = dfs1(v);
      if (x) return x;
    }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = (1); i <= (n); ++i) {
    int p;
    cin >> p;
    g[i].emplace_back(p);
  }
  for (int i = (1); i <= (n); ++i)
    if (!num[i]) dfs(i);
  for (int i = (1); i <= (ncc); ++i) {
    vector<int> change;
    for (int u : scc[i])
      for (int v : g[u])
        if (cc[v] != i && !in[cc[v]]) in[cc[v]] = 1, change.emplace_back(cc[v]);
    for (int v : change) indeg[v] = 1, gr[i].emplace_back(v), in[v] = 0;
  }
  for (int i = (1); i <= (ncc); ++i)
    if (!indeg[i]) {
      int x = dfs1(i);
      if (x) {
        st.emplace_back(i);
        ed.emplace_back(x);
      } else
        unst.emplace_back(i);
    }
  for (int i = (1); i <= (ncc); ++i)
    if (((int)(gr[i]).size()) == 0 && !in[i]) uned.emplace_back(i);
  vector<pair<int, int> > ans;
  for (int i = (0); i <= (((int)(st).size()) - 2); ++i) {
    if (ed[i] != st[i + 1]) ans.emplace_back(scc[ed[i]][0], scc[st[i + 1]][0]);
  }
  if (ed.back() != st[0]) ans.emplace_back(scc[ed.back()][0], scc[st[0]][0]);
  for (int i = (0); i <= (min(((int)(unst).size()), ((int)(uned).size())) - 1);
       ++i) {
    if (uned[i] != unst[i]) ans.emplace_back(scc[uned[i]][0], scc[unst[i]][0]);
  }
  if (((int)(unst).size()) > ((int)(uned).size()))
    for (int i = (((int)(uned).size())); i <= (((int)(unst).size()) - 1); ++i)
      ans.emplace_back(scc[st[0]][0], scc[unst[i]][0]);
  else
    for (int i = (((int)(unst).size())); i <= (((int)(uned).size()) - 1); ++i)
      ans.emplace_back(scc[uned[i]][0], scc[st[0]][0]);
  cout << ((int)(ans).size()) << '\n';
  for (pair<int, int> v : ans) cout << v.first << ' ' << v.second << '\n';
  return 0;
}
