#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
vector<int> g[(100005)], G[(100005)], e[(100005)], R[(100005)];
int cn[(100005)], C[(100005)], I[(100005)], H[(100005)], L;
void CLR(int N = (100005)) {
  L = 0;
  for (int i(0); i < N; i++)
    g[i].clear(), e[i].clear(), G[i].clear(), R[i].clear(), cn[i] = I[i] = 0;
}
void dfs1(int x) {
  cn[x] = 1;
  for (auto h : g[x])
    if (!cn[h]) dfs1(h);
  H[L++] = x;
}
void dfs2(int x, int c) {
  cn[x] = 0, C[x] = c;
  for (auto h : e[x])
    if (cn[h]) dfs2(h, c);
}
int ini(int N) {
  for (int i(0); i < N; i++)
    if (!cn[i]) dfs1(i);
  int I(0);
  for (int i(L - 1); ~i; --i)
    if (cn[H[i]]) dfs2(H[i], ++I);
  for (int i(0); i < N; i++) --C[i];
  for (int i(0); i < N; i++)
    for (auto &h : g[i])
      if (C[i] != C[h]) G[C[i]].push_back(C[h]), R[C[h]].push_back(C[i]);
  return I;
}
int N, A[(100005)], M, U[(100005)], V[(100005)], Y[(100005)], r;
vector<int> F, T;
vector<pair<int, int> > o;
void dfs(int u, int r) {
  Y[u] = r;
  if (R[u].empty()) F.push_back(u);
  for (auto &h : R[u]) dfs(h, r);
}
int main(void) {
  scanf("%d", &N), ga(N, A), CLR(N);
  for (int i(0); i < N; i++) --A[i];
  for (int i(0); i < N; i++) (g[i].push_back(A[i]), e[A[i]].push_back(i));
  M = ini(N);
  if (M == 1) return puts("0");
  for (int i(0); i < N; i++) U[C[i]] = i;
  for (int i(0); i < M; i++)
    if (G[i].empty()) T.push_back(i);
  for (auto &h : T) dfs(h, h);
  r = T.back();
  for (auto &h : F) o.push_back({r, h}), r = Y[h];
  printf("%lu\n", o.size());
  for (auto &h : o) printf("%d %d\n", U[h.first] + 1, U[h.second] + 1);
  return 0;
}
