#include <bits/stdc++.h>
using namespace std;
int target[100100];
int indeg[100100];
bool visited[100100];
int cycleNode[100100];
int cycleId[100100];
int represent[100100];
int start;
vector<int> dependsOn[100100];
vector<pair<long long int, long long int> > edges;
int cyclec = 0;
bool newcycle = false;
bool hasindeg = false;
int dfs(int node) {
  visited[node] = true;
  if (visited[target[node]]) {
    if (cycleId[target[node]] == -1) {
      cycleId[node] = cyclec;
      newcycle = true;
      represent[cyclec] = node;
      return cyclec;
    } else {
      cycleId[node] = cycleId[target[node]];
      return cycleId[target[node]];
    }
  }
  int ret = dfs(target[node]);
  cycleId[node] = ret;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  memset(cycleId, -1, sizeof cycleId);
  memset(cycleNode, -1, sizeof cycleNode);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> target[i];
    target[i]--;
    indeg[target[i]]++;
  }
  for (int i = 0; i < n; ++i) {
    if (indeg[i] == 0) {
      hasindeg = true;
      newcycle = false;
      start = i;
      dfs(i);
      if (newcycle == true) {
        cyclec++;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (cycleId[i] == -1) {
      newcycle = false;
      start = i;
      dfs(i);
      if (newcycle == true) {
        cyclec++;
      }
      dependsOn[cycleId[i]].push_back(i);
    }
  }
  for (int i = 0; i < n; ++i) {
    if (indeg[i] == 0 && cycleId[i] != -1) dependsOn[cycleId[i]].push_back(i);
  }
  if (cyclec == 1) {
    if (!hasindeg) {
      cout << 0 << endl;
      return 0;
    }
  } else {
    for (int i = 1; i < cyclec; ++i) {
      for (int u : dependsOn[i - 1]) {
        edges.push_back({represent[i], u});
      }
    }
  }
  for (int u : dependsOn[cyclec - 1]) {
    edges.push_back({represent[0], u});
  }
  cout << edges.size() << endl;
  for (int i = 0; i < edges.size(); ++i) {
    cout << edges[i].first + 1 << " " << edges[i].second + 1 << endl;
  }
  return 0;
}
