#include <bits/stdc++.h>
using namespace std;
int used[100001];
vector<int> g[100001];
int d[100001];
int n;
vector<int> start, finish;
int DFS(int v) {
  used[v] = 1;
  for (int x : g[v])
    if (used[x] == 0) return DFS(x);
  return v;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    g[i].push_back(x);
    d[x] = 1;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (d[i] == 0) {
      cnt++;
      start.push_back(i);
      ;
      finish.push_back(DFS(i));
    }
  }
  int cnt1 = 0;
  for (int i = 1; i <= n; i++) {
    if (used[i] == 0) {
      cnt1++;
      start.push_back(i);
      finish.push_back(DFS(i));
    }
  }
  if (cnt1 == 1 && cnt == 0) {
    cout << "0";
    return 0;
  }
  cout << cnt + cnt1 << endl;
  for (int i = 0; i < start.size(); i++) {
    if (i == start.size() - 1) {
      cout << finish[i] << " " << start[0] << endl;
    } else
      cout << finish[i] << " " << start[i + 1] << endl;
  }
}
