#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5;
int n;
int c[maxn];
vector<pair<int,int>> res;
void cswap(int pos1,int pos2){
	res.push_back({pos1,pos2});
	swap(c[pos1],c[pos2]);
	c[pos1]=~c[pos1];
	c[pos2]=~c[pos2];
}
void solve(){
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> c[i];
		c[i]--;
	}
	vector<vector<int>> cycle;
	vector<bool> vis(n,false);
	for(int i=0;i<n;i++){
		if(vis[i]) continue;
		vector<int> cyc;
		int j=i;
		while(!vis[j]){
			cyc.push_back(j);
			vis[j]=true;
			j=c[j];
		}
		cycle.push_back(cyc);
	}
	while(cycle.size()>=2){
		vector<int> cyc1=cycle.back();
		cycle.pop_back();
		vector<int> cyc2=cycle.back();
		cycle.pop_back();
		for(int i=0;i<cyc1.size();i++)
			cswap(cyc1[i],cyc2[0]);
		for(int i=1;i<cyc2.size();i++)
			cswap(cyc1[0],cyc2[i]);
		cswap(cyc1[0],cyc2[0]);
	}
	if(cycle.size()){
		vector<int> cyc=cycle[0];
		if(cyc.size()==2){
			int other=c[0] ? (c[1] ? 1 : 2) : 0;
			cswap(cyc[0],other);
			cswap(cyc[1],other);
			cswap(cyc[0],other);
		}
		if(cyc.size()>2){
			cswap(cyc[0],cyc[1]);
			for(int i=2;i<cyc.size();i++)
				cswap(cyc[1],cyc[i]);
			cswap(cyc[0],cyc[2]);
			cswap(cyc[0],cyc[1]);
		}
	}
	cout << res.size() << endl;
	for(pair<int,int> operation: res)
		cout << operation.first+1 << ' ' << operation.second+1 << endl;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t=1;
	while(t--)
		solve();
	return 0;
}