#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long mod = 998244353;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long binpow(long long a, long long b, long long m) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
int n, m;
vector<vector<int>> g, gt;
vector<int> used;
vector<int> order, comp;
void dfs1(int v) {
  used[v] = true;
  for (int u : g[v]) {
    if (!used[u]) dfs1(u);
  }
  order.push_back(v);
}
void dfs2(int v, int cl) {
  comp[v] = cl;
  for (int u : gt[v]) {
    if (comp[u] == -1) dfs2(u, cl);
  }
}
vector<vector<int>> dag;
vector<vector<int>> components;
vector<int> vis;
vector<int> A, B;
int w;
bool sinknotfound;
vector<int> in, out;
void search(int x) {
  vis[x] = 1;
  if (out[x] == 0) {
    w = x;
    sinknotfound = false;
    return;
  }
  for (int y : dag[x]) {
    if (sinknotfound && !vis[y]) search(y);
  }
}
void kosaraju() {
  used.assign(n, 0);
  for (int i = 0; i < n; ++i) {
    if (!used[i]) dfs1(i);
  }
  comp.assign(n, -1);
  int id = 0;
  for (int i = 0; i < n; ++i) {
    int v = order[n - i - 1];
    if (comp[v] == -1) dfs2(v, id++);
  }
  if (id == 1) {
    cout << 0 << "\n";
    return;
  }
  dag.resize(id, vector<int>());
  components = dag;
  for (int i = 0; i < n; i++) {
    components[comp[i]].push_back(i);
  }
  fill(used.begin(), used.end(), 0);
  in.resize(id);
  out = in;
  for (int i = 0; i < id; i++) {
    for (int u : components[i]) {
      for (int v : g[u]) {
        if (comp[v] != i && !used[comp[v]]) {
          used[comp[v]] = 1;
          dag[i].push_back(comp[v]);
          in[comp[v]]++;
          out[i]++;
        }
      }
    }
    for (int u : components[i]) {
      for (int v : g[u])
        if (used[comp[v]]) used[comp[v]] = 0;
    }
  }
  vis.assign(id, 0);
  vector<int> sources, sinks, uss(id);
  int t1 = 0, t2 = 0;
  for (int i = 0; i < id; i++) {
    if (in[i] == 0) {
      w = -1;
      sinknotfound = true;
      search(i);
      if (!sinknotfound) {
        A.push_back(components[i][0] + 1);
        B.push_back(components[w][0] + 1);
        uss[i] = 1;
        uss[w] = 1;
      }
      t1++;
    }
    if (out[i] == 0) {
      t2++;
    }
  }
  cout << max(t1, t2) << "\n";
  int N = A.size();
  for (int j = 0; j < N - 1; j++) {
    cout << B[j] << " " << A[j + 1] << "\n";
  }
  cout << B[N - 1] << " " << A[0] << "\n";
  for (int i = 0; i < id; i++) {
    if (!uss[i] && out[i] == 0) {
      sinks.push_back(components[i][0] + 1);
    }
    if (!uss[i] && in[i] == 0) {
      sources.push_back(components[i][0] + 1);
    }
  }
  n = sources.size();
  m = sinks.size();
  if (n >= m) {
    for (int i = 0; i < m; i++) {
      cout << sinks[i] << " " << sources[i] << "\n";
    }
    for (int i = m; i < n; i++) {
      cout << B[0] << " " << sources[i] << "\n";
    }
  } else {
    for (int i = 0; i < n; i++) {
      cout << sinks[i] << " " << sources[i] << "\n";
    }
    for (int i = n; i < m; i++) {
      cout << sinks[i] << " " << A[0] << "\n";
    }
  }
}
void solve() {
  cin >> n;
  g = gt = vector<vector<int>>(n, vector<int>());
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    g[i].push_back(x);
    gt[x].push_back(i);
  }
  kosaraju();
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cout << fixed << setprecision(12);
  while (t--) {
    solve();
  }
}
