#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > t;
int n, out[1000000], nxt[1000000], k = 0, vis[1000000];
int dfs(int x) {
  vis[x] = 1;
  if (!vis[nxt[x]])
    return vis[x] = dfs(nxt[x]);
  else
    return vis[x] = x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &nxt[i]);
    out[nxt[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!out[i]) {
      k++;
      int x = dfs(i);
      t.push_back(make_pair(x, i));
    }
  }
  int kk = k;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      k++;
      int x = dfs(i);
      t.push_back(make_pair(x, i));
    }
  }
  if (k == 1 && !kk) k = 0;
  printf("%d\n", k);
  for (int i = 0; i < k; i++)
    printf("%d %d\n", t[i].first, t[(i + 1) % k].second);
  return 0;
}
