#include <bits/stdc++.h>
const int N = 1e6 + 10;
const int M = 1e3 + 7;
const int MOD = 1e9 + 7;
using namespace std;
vector<vector<long long>> adj(N);
vector<pair<long long, long long>> adj2[N];
vector<pair<long long, long long>> store;
vector<long long> vis(N), out(N);
vector<long long> vx;
vector<long long> a(N);
vector<long long> ans;
set<long long> second;
long long n, m, k, cnt, dis = 0, ind = 0;
bool visx[M][M];
int used[M][M];
char ch[M][M];
vector<int> ax(N), bx(N), deg(N), b(N);
int idx[] = {0, -1, 0, 1};
int idy[] = {1, 0, -1, 0};
inline bool in(int x, int y) {
  return 1 <= x && x < n + 1 && 1 <= y && y < m + 1;
}
long long power(long long a, long long n, long long m = MOD) {
  if (n == 0) return 1;
  long long p = power(a, n >> 1, m);
  p = (p * p) % m;
  if (n & 1) p = (p * a) % m;
  return p;
}
void dfs2(long long x, long long y = 0) {
  vis[x] = 1;
  y += ax[x];
  bx[x] = y;
  for (auto i : adj[x]) {
    if (!vis[i]) dfs2(i, y);
  }
}
void dfs3(int i, int p = -1) {
  for (auto j : adj[i])
    if (j != p) {
      ax[j] += ax[i];
      dfs3(j, i);
    }
}
void dfs(long long x, long long y) {
  if (!in(x, y) || ch[x][y] == '*' || visx[x][y]) return;
  visx[x][y] = 1;
  cnt++;
  used[x][y] = ind;
  for (int i = 0; i < 4; i++) dfs(x + idx[i], y + idy[i]);
}
void dfsz(long long x, long long y, long long z) {
  vis[x] = 1;
  if (a[x] ^ y != b[x]) {
    vx.push_back(x);
    y = !y;
  }
  for (auto i : adj[x])
    if (!vis[i]) dfsz(i, z, y);
}
int dfs4(long long x1, long long y1) {
  long long dpt = 0;
  vis[x1] = 1;
  for (auto i : adj2[x1]) {
    if (!vis[i.first]) dpt += dfs4(i.first, i.second);
  }
  if (y1 == 2 && dpt == 0) ans.push_back(x1);
  if (y1 == 2) return 1;
  return min((long long)1, dpt);
}
void cyclicdfs(long long x) {
  vis[x] = 1;
  ind = x;
  if (!vis[bx[x]]) cyclicdfs(bx[x]);
}
int main() {
  int n;
  cin >> n;
  int cycle = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    deg[a[i]]++;
    bx[i] = a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0 && deg[i] == 0) {
      cyclicdfs(i);
      store.push_back(make_pair(i, ind));
    }
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      cyclicdfs(i);
      cycle++;
      store.push_back(make_pair(i, ind));
    }
  }
  if (store.size() == cycle && cycle == 1) return cout << 0, 0;
  cout << store.size() << "\n";
  for (int i = 0; i < store.size(); i++) {
    cout << store[i].second << " " << store[(i + 1) % store.size()].first
         << "\n";
  }
  return 0;
}
