#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
#define f first
#define s second
const int nax=2e5+100;
int a[nax],vis[nax],n;
vector<pi> res;
void check(int i,int j)
{
    swap(a[i],a[j]);
    a[i]*=-1;
    a[j]*=-1;
    res.push_back({i,j});
}
void find(int i,int j)
{
    check(i,j);
    int cur=i;
    while(a[-a[cur]]>0)
    check(cur,-a[cur]);
    cur=-a[cur];
    while(a[-a[cur]]>0)
    check(cur,-a[cur]);
    check(cur,-a[cur]);
}
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    int p=-1;
    for(int i=1;i<=n;i++)
    if(!vis[i])
    {
        if(a[i]==i)
        continue;
        int cur=i;
        do
        {
            vis[cur]=1;
            cur=a[cur];
        }while(cur!=i);
        if(p==-1)
        p=i;
        else
        {
            find(p,i);
            p=-1;
        }
    }
    if(p!=-1)
    {
        bool f=0;
        for(int i=1;i<=n;i++)
        if(a[i]==i)
        {
            find(p,i);
            f=1;
            break;
        }
        if(!f)
        {
            int t1=a[p],t2=a[a[p]];
            check(p,t1);
            find(t1,t2);
        }
    }
    cout<<res.size()<<'\n';
    for(auto x:res)
    cout<<x.f<<' '<<x.s<<'\n';
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int t=1;
    //cin>>t;
    while(t--)
    solve();
    return 0;
}