#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > ans;
vector<int> g[100005], comp;
bool visited[100005], deg[100005];
void dfs(int s, int u) {
  visited[u] = true;
  for (int v : g[u]) {
    if (!visited[v])
      dfs(s, v);
    else
      ans.push_back({v, s});
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int x;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    g[i].push_back(x);
    deg[x] = true;
  }
  for (int i = 1; i <= n; ++i)
    if (!deg[i]) dfs(i, i);
  for (int i = 1; i <= n; ++i)
    if (!visited[i]) dfs(i, i);
  vector<pair<int, int> > fin;
  for (int i = 1; i < (int((ans).size())); ++i)
    fin.push_back({ans[i - 1].first, ans[i].second});
  if ((int((ans).size())) > 1 or
      ((int((ans).size())) > 0 and
       ans[(int((ans).size())) - 1].first != ans[0].second))
    fin.push_back({ans[(int((ans).size())) - 1].first, ans[0].second});
  cout << (int((fin).size())) << '\n';
  for (pair<int, int> s : fin) cout << s.first << " " << s.second << '\n';
  return 0;
}
