#include <bits/stdc++.h>
using namespace std;
int ini() {
  int a;
  scanf("%d", &a);
  return a;
}
long long inl() {
  long long a;
  scanf("%lld", &a);
  return a;
}
char inc() {
  char a;
  scanf("%c", &a);
  return a;
}
void readln() { scanf("\n"); }
int n, deg[111222], p[111222], ke[111222], m, a[111222], res;
vector<int> ds[111222];
pair<int, int> r[111222];
int visit(int u) {
  p[u] = n;
  int v = ke[u];
  if (p[v] == n) return p[u] = v;
  if (p[v] > -1) return p[u] = p[v];
  return p[u] = visit(v);
}
int main() {
  if (0) {
    freopen("aaa.inp", "r", stdin);
    freopen("aaa.out", "w", stdout);
  };
  n = ini();
  for (int u = 0; u < (int)n; u++) {
    int v = ini() - 1;
    ke[u] = v;
    deg[v]++;
  }
  for (int i = 0; i < (int)n; i++) p[i] = -1;
  for (int i = 0; i < (int)n; i++)
    if (p[i] < 0) visit(i);
  for (int i = 0; i < (int)n; i++)
    if (p[i] == i) a[m++] = i;
  for (int i = 0; i < (int)n; i++)
    if (!deg[i]) ds[p[i]].push_back(i);
  if (m == 1 && ds[a[0]].size() == 0) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < (int)n; i++)
    if (p[i] == i && ds[i].size() == 0) ds[i].push_back(i);
  for (int i = 0; i < (int)m - 1; i++)
    r[res++] = make_pair(a[i], ds[a[i + 1]][0]);
  r[res++] = make_pair(a[m - 1], ds[a[0]][0]);
  for (int i = 0; i < (int)m; i++)
    for (int j = 1; j < ds[a[i]].size(); j++)
      r[res++] = make_pair(a[i], ds[a[i]][j]);
  printf("%d\n", res);
  for (int i = 0; i < (int)res; i++)
    printf("%d %d\n", r[i].first + 1, r[i].second + 1);
  return 0;
}
