#include <bits/stdc++.h>
using namespace std;
int z = 0, insp, beg, end1;
vector<int> ed[100001], tr[100001], ins;
vector<int> vis(100001), out(100001);
void dfs1(int node) {
  vis[node] = 1;
  for (int child : ed[node]) {
    if (vis[child] == 0) {
      dfs1(child);
    } else if (vis[child] == 1) {
      end1 = child;
      break;
    }
  }
}
int main() {
  int n, i, tinp, j, k = 0;
  cin >> n;
  vector<int> input(n), in(n + 1, 0), b, e;
  for (i = 0; i < n; i++) {
    cin >> input[i];
    ed[i + 1].push_back(input[i]);
    in[input[i]]++;
  }
  for (i = 1; i <= n; i++) {
    if (in[i] == 0 && vis[i] == 0) {
      dfs1(i);
      b.push_back(i);
      e.push_back(end1);
    }
  }
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      k++;
      b.push_back(i);
      dfs1(i);
      e.push_back(end1);
    }
  }
  if (k == 1 && b.size() == 1) b.pop_back();
  e.pop_back();
  cout << b.size() << endl;
  for (i = 0; i < b.size(); i++) {
    cout << e[i] << " " << b[(i + 1) % b.size()] << endl;
  }
}
