#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  T r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
int getbit(T s, int i) {
  return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
  return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
  return s & (~(T(1) << i));
}
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
const int infi = 1e9;
const long long Linfi = (long long)9e18;
const long long MOD = 1000000007;
const int c1 = 11;
const int c2 = 37;
int n, m, t, Count = 0, ans = 0;
vector<int> adj[100005];
int inCome[100005], xet[100005], f[100005];
vector<pair<long long, long long> > V;
int DFS(int u) {
  if (xet[u]) return u;
  xet[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    return DFS(v);
  }
}
void solve() {
  memset(xet, 0, sizeof(xet));
  memset(inCome, 0, sizeof(inCome));
  for (int u = 1; u <= n; u++) {
    int v = adj[u][0];
    inCome[v]++;
  }
  for (int u = 1; u <= n; u++) {
    if (inCome[u] == 0) {
      int to = DFS(u);
      V.push_back(pair<long long, long long>(to, u));
    }
  }
  for (int u = 1; u <= n; u++) {
    if (xet[u] == 0) {
      int to = DFS(u);
      V.push_back(pair<long long, long long>(to, u));
    }
  }
  if (V.size() == 1) {
    if (V[0].first == V[0].second)
      cout << 0 << endl;
    else {
      cout << 1 << endl << V[0].first << " " << V[0].second << endl;
    }
    return;
  }
  int ans = V.size();
  cout << ans << endl;
  for (int i = 0; i < ans; i++)
    cout << V[i].first << " " << V[(i + 1) % ans].second << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> f[i];
    int u = f[i];
    adj[i].push_back(u);
  }
  solve();
  return 0;
}
