#include <bits/stdc++.h>
using namespace std;
static const int MAXN = 1e5 + 10;
int son[MAXN], d[MAXN];
bool vis[MAXN];
int n;
vector<int> h, t;
int dfs(int x) {
  vis[x] = true;
  if (!vis[son[x]]) return dfs(son[x]);
  return x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &son[i]);
    d[son[i]]++;
  }
  for (int i = 1; i <= n; i++)
    if (!d[i]) {
      h.push_back(i);
      t.push_back(dfs(i));
    }
  int cnt0 = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      cnt0++;
      h.push_back(i);
      t.push_back(dfs(i));
    }
  int cnt = h.size();
  if (cnt == 1 && cnt0 == 1) cnt = 0;
  printf("%d\n", cnt);
  for (int i = 0; i < cnt; i++) printf("%d %d\n", t[i], h[(i + 1) % cnt]);
  return 0;
}
