#include <bits/stdc++.h>
using namespace std;
int n, used = 0, nxt[100010], in[100010], fa[100010];
vector<int> l1, l2;
int dfs(int x) {
  fa[x] = 1;
  if (!fa[nxt[x]])
    return fa[x] = dfs(nxt[x]);
  else
    return fa[x] = x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &nxt[i]);
    in[nxt[i]]++;
  }
  for (int i = 1; i <= n; i++)
    if (!in[i]) {
      l1.push_back(i);
      l2.push_back(dfs(i));
    }
  used = l1.size();
  for (int i = 1; i <= n; i++)
    if (!fa[i]) {
      l1.push_back(i);
      l2.push_back(dfs(i));
    }
  if (l1.size() == 1 && used == 0) {
    printf("0\n");
    return 0;
  }
  printf("%d\n", l1.size());
  for (int i = 0; i < l1.size(); i++)
    printf("%d %d\n", l2[i], l1[(i + 1) % l1.size()]);
  return 0;
}
