#include <bits/stdc++.h>
using namespace std;
int n;
const int mxn = 1e5 + 5;
int d[mxn], f[mxn];
bool vis[mxn];
vector<int> head, tail;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> f[i];
    ++d[f[i]];
  }
  function<int(int)> dfs = [&](int x) {
    vis[x] = true;
    if (!vis[f[x]]) {
      return dfs(f[x]);
    }
    return x;
  };
  for (int i = 1; i <= n; ++i) {
    if (!d[i]) {
      head.push_back(i);
      tail.push_back(dfs(i));
    }
  }
  int cycle = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      ++cycle;
      head.push_back(i);
      tail.push_back(dfs(i));
    }
  }
  int all = head.size();
  if (cycle == 1 && all == 1) {
    cout << 0 << '\n';
    return 0;
  }
  cout << all << '\n';
  for (int i = 0; i < all; ++i) {
    cout << tail[i] << ' ' << head[(i + 1) % all] << '\n';
  }
  return 0;
}
