#include <bits/stdc++.h>
const int N = 100010;
const int inf = 0x3f3f3f3f;
using namespace std;
int f[N], n, in[N];
vector<int> zero, out;
int vis[N];
int dfs(int u, int r) {
  if (vis[u] == r) return u;
  vis[u] = r;
  return dfs(f[u], r);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &f[i]), in[f[i]]++;
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) cnt++, zero.push_back(i), out.push_back(dfs(i, i));
  for (int i = 1; i <= n; i++)
    if (vis[i] == 0) zero.push_back(i), out.push_back(i), dfs(i, i);
  int sz = zero.size();
  if (sz == 1 && cnt == 0) {
    printf("0\n");
    return 0;
  }
  printf("%d\n", sz);
  if (sz) printf("%d %d\n", out[sz - 1], zero[0]);
  for (int i = 0; i < sz - 1; i++) {
    printf("%d %d\n", out[i], zero[i + 1]);
  }
  return 0;
}
