#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
int n, C = 1, ans;
int comp[MAX_N];
bool Mark[MAX_N];
vector<int> G[MAX_N];
vector<int> Grev[MAX_N];
vector<int> Gcmp[MAX_N];
vector<int> Gcmprev[MAX_N];
vector<int> topo;
vector<int> sink;
vector<int> S[MAX_N];
int T[MAX_N];
int ver[MAX_N];
int dfs(int v) {
  Mark[v] = true;
  for (int i = 0; i < G[v].size(); i++)
    if (!Mark[G[v][i]]) dfs(G[v][i]);
  topo.push_back(v);
  return 0;
}
void dfs_rev(int v, int c) {
  comp[v] = c;
  ver[c] = v;
  for (int i = 0; i < Grev[v].size(); i++)
    if (!comp[Grev[v][i]]) dfs_rev(Grev[v][i], c);
}
void dfs_cmprev(int v, int s) {
  if (Gcmprev[v].size() == 0) {
    S[s].push_back(v);
    T[v] = s;
  }
  for (int i = 0; i < Gcmprev[v].size(); i++) dfs_cmprev(Gcmprev[v][i], s);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    v--;
    G[i].push_back(v);
    Grev[v].push_back(i);
  }
  for (int i = 0; i < n; i++)
    if (!Mark[i]) dfs(i);
  reverse(topo.begin(), topo.end());
  for (int i = 0; i < n; i++)
    if (!comp[topo[i]]) dfs_rev(topo[i], C++);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < G[i].size(); j++)
      if (comp[i] != comp[G[i][j]]) {
        Gcmp[comp[i]].push_back(comp[G[i][j]]);
        Gcmprev[comp[G[i][j]]].push_back(comp[i]);
      }
  for (int i = 1; i < C; i++)
    if (Gcmp[i].size() == 0) sink.push_back(i);
  for (int i = 0; i < sink.size(); i++) dfs_cmprev(sink[i], sink[i]);
  for (int i = 1; i < C; i++) ans += (Gcmprev[i].size() == 0);
  if (sink.size() == 1) {
    if (C == 2) {
      cout << "0\n";
      return 0;
    }
    cout << ans << "\n";
    for (int i = 1; i < C; i++)
      if (Gcmprev[i].size() == 0)
        cout << ver[T[i]] + 1 << " " << ver[i] + 1 << "\n";
  } else {
    cout << ans << "\n";
    for (int i = 0; i < sink.size(); i++) {
      int u = sink[i];
      int nxt = sink[(i + 1) % sink.size()];
      for (int j = 0; j < S[u].size(); j++)
        cout << ver[nxt] + 1 << " " << ver[S[u][j]] + 1 << "\n";
    }
  }
  return 0;
}
