#include <bits/stdc++.h>
using namespace std;
void mytimer(string task) {}
int src[110000];
bool was[110000];
vector<int> loops[110000];
vector<int> loops2, loops3;
vector<int> resv[110000];
int rlen;
int cnt;
int n;
int main() {
  cin >> n;
  for (int i = 0; i < (n); ++i) cin >> src[i], --src[i];
  for (int i = 0; i < (n); ++i) {
    if (was[i]) continue;
    for (int j = i; !was[j]; j = src[j]) {
      loops[cnt].push_back(j);
      was[j] = true;
    }
    ++cnt;
  }
  for (int i = 0; i < (cnt); ++i) {
    vector<int> &loop = loops[i];
    while (loop.size() > 3) {
      vector<int> &res = resv[rlen++];
      int l = min(5, (int)loop.size());
      for (int j = 0; j < (l); ++j) res.push_back(loop[loop.size() - l + j]);
      for (int j = 0; j < (l); ++j)
        res.push_back(loop[loop.size() - l + (j + 1) % l]);
      for (int j = 0; j < (l - 1); ++j) loop.pop_back();
    }
    if (loop.size() == 2) loops2.push_back(i);
    if (loop.size() == 3) loops3.push_back(i);
  }
  while (loops3.size() > loops2.size() && loops3.size() >= 2) {
    vector<int> &res = resv[rlen++];
    int l1i = loops3.back();
    vector<int> l1 = loops[l1i];
    loops3.pop_back();
    int l2i = loops3.back();
    vector<int> l2 = loops[l2i];
    loops3.pop_back();
    res.push_back(l1[0]);
    res.push_back(l1[1]);
    res.push_back(l1[2]);
    res.push_back(l2[1]);
    res.push_back(l2[2]);
    res.push_back(l1[1]);
    res.push_back(l1[2]);
    res.push_back(l1[0]);
    res.push_back(l2[2]);
    res.push_back(l2[1]);
    loops[l2i].pop_back();
    loops2.push_back(l2i);
  }
  while (loops2.size() && loops3.size()) {
    vector<int> &res = resv[rlen++];
    vector<int> &l2 = loops[loops2.back()];
    vector<int> &l3 = loops[loops3.back()];
    loops2.pop_back();
    loops3.pop_back();
    res.push_back(l2[0]);
    res.push_back(l2[1]);
    res.push_back(l3[0]);
    res.push_back(l3[1]);
    res.push_back(l3[2]);
    res.push_back(l2[1]);
    res.push_back(l2[0]);
    res.push_back(l3[1]);
    res.push_back(l3[2]);
    res.push_back(l3[0]);
  }
  while (loops3.size()) {
    vector<int> &res = resv[rlen++];
    vector<int> l = loops[loops3.back()];
    loops3.pop_back();
    res.push_back(l[0]);
    res.push_back(l[1]);
    res.push_back(l[2]);
    res.push_back(l[1]);
    res.push_back(l[2]);
    res.push_back(l[0]);
  }
  while (loops2.size() >= 2) {
    vector<int> &res = resv[rlen++];
    vector<int> l1 = loops[loops2.back()];
    loops2.pop_back();
    vector<int> l2 = loops[loops2.back()];
    loops2.pop_back();
    res.push_back(l1[0]);
    res.push_back(l1[1]);
    res.push_back(l2[0]);
    res.push_back(l2[1]);
    res.push_back(l1[1]);
    res.push_back(l1[0]);
    res.push_back(l2[1]);
    res.push_back(l2[0]);
  }
  while (loops2.size()) {
    vector<int> &res = resv[rlen++];
    vector<int> l = loops[loops2.back()];
    loops2.pop_back();
    res.push_back(l[0]);
    res.push_back(l[1]);
    res.push_back(l[1]);
    res.push_back(l[0]);
  }
  cout << rlen << '\n';
  for (int i = 0; i < (rlen); ++i) {
    cout << resv[i].size() / 2 << '\n';
    for (int j = 0; j < (resv[i].size() / 2); ++j)
      cout << resv[i][j] + 1 << ' ';
    cout << '\n';
    for (int j = 0; j < (resv[i].size() / 2); ++j)
      cout << resv[i][resv[i].size() / 2 + j] + 1 << ' ';
    cout << '\n';
  }
  return 0;
}
