#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const LL inf  = 1e18+9;
const LL mod  = 1e9+7;
const LL maxn = 2e5+8;

LL n, m, c[maxn], vs[maxn], block[maxn], belong[maxn], sz[maxn], tot;

void process(vector<pair<LL, LL>> &vc, LL x, LL y) {
    LL ox = x, oy = y;
    swap(x, y);
    vc.push_back({x, y});
    while (c[x] != oy) vc.push_back({ox, c[x]}), x = c[x];
    while (c[y] != ox) vc.push_back({oy, c[y]}), y = c[y];
    vc.push_back({ox, oy});
}

void solve() {
    LL i, j, x, y;
    scanf("%lld", &n);
    vector<pair<LL, LL>> vc;
    for (i = 1; i <= n; i++) scanf("%lld", &c[i]);
    for (i = 1; i <= n; i++) {
        if (c[i] == i) vs[i] = 1;
        else if (!vs[i]) {
            block[++tot] =  j = i;
            do {
                sz[i]++;
                belong[j] = i; vs[j] = 1; j = c[j];
            } while (j != i);
        }
    }
    if (tot & 1) {
        x = block[tot];
        if (sz[block[tot]] == 2) {
            for (y = 1; belong[y] == x; y++);
            vc.push_back({x, y});
            vc.push_back({y, c[x]});
            vc.push_back({x, y});
        }
        else {
            y = c[x];
            LL ox = x, oy = y;
            vc.push_back({x, y});
            swap(x, y);
            while (c[c[x]] != ox) vc.push_back({ox, c[x]}), x = c[x];
            vc.push_back({oy, c[x]});
            vc.push_back({ox, c[x]});
            vc.push_back({ox, oy});
        }
    }
    for (i = 1; i < tot; i += 2) process(vc, block[i], block[i + 1]);
    printf("%lld\n", (LL)vc.size());
    for (auto it : vc) printf("%lld %lld\n", it.first, it.second);
}

signed main() {
    LL t = 1; //scanf("%lld", &t);
    while (t--) solve();
    return 0;
}

