#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
  return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
  return a * a;
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
const int INF = 1000 * 1000 * 1000;
const int N = 100 * 1000 + 13;
int n;
int p[N];
queue<int> cyc[N];
int szcyc;
set<pair<int, int> > s;
int used[N], U = 0;
vector<int> ans1[N], ans2[N];
int szans = 0;
int main() {
  cin >> n;
  for (int i = 0; i < int(n); i++) scanf("%d", &p[i]), p[i]--;
  U++;
  for (int i = 0; i < int(n); i++) {
    if (used[i] == U || p[i] == i) continue;
    int v = i;
    while (used[v] != U) {
      cyc[szcyc].push(v);
      used[v] = U;
      v = p[v];
    }
    s.insert(make_pair((int((cyc[szcyc]).size())), (szcyc)));
    szcyc++;
  }
  while (!s.empty() && (--s.end())->first > 3) {
    int num = (--s.end())->second;
    s.erase(--s.end());
    if (int((cyc[num]).size()) <= 5) {
      while (!cyc[num].empty()) {
        int cur = cyc[num].front();
        cyc[num].pop();
        ans1[szans].push_back(cur);
        ans2[szans].push_back(p[cur]);
      }
    } else {
      for (int i = 0; i < int(5); i++) {
        int cur = cyc[num].front();
        cyc[num].pop();
        if (i == 4) {
          ans1[szans].push_back(cur);
          ans2[szans].push_back(cyc[num].back());
          p[cyc[num].back()] = p[cur];
        } else {
          ans1[szans].push_back(cur);
          ans2[szans].push_back(p[cur]);
          if (i == 0) cyc[num].push(cur);
        }
      }
      s.insert(make_pair((int((cyc[num]).size())), (num)));
    }
    szans++;
  }
  while (!s.empty() && s.begin()->first == 2 && (--s.end())->first == 3) {
    int num = s.begin()->second;
    s.erase(s.begin());
    while (!cyc[num].empty()) {
      int cur = cyc[num].front();
      cyc[num].pop();
      ans1[szans].push_back(cur);
      ans2[szans].push_back(p[cur]);
    }
    num = (--s.end())->second;
    s.erase(--s.end());
    while (!cyc[num].empty()) {
      int cur = cyc[num].front();
      cyc[num].pop();
      ans1[szans].push_back(cur);
      ans2[szans].push_back(p[cur]);
    }
    szans++;
  }
  if (!s.empty() && (--s.end())->first == 2) {
    while (!s.empty()) {
      int num = s.begin()->second;
      s.erase(s.begin());
      while (!cyc[num].empty()) {
        int cur = cyc[num].front();
        cyc[num].pop();
        ans1[szans].push_back(cur);
        ans2[szans].push_back(p[cur]);
      }
      if (!s.empty()) {
        num = s.begin()->second;
        s.erase(s.begin());
        while (!cyc[num].empty()) {
          int cur = cyc[num].front();
          cyc[num].pop();
          ans1[szans].push_back(cur);
          ans2[szans].push_back(p[cur]);
        }
      }
      szans++;
    }
  } else if (!s.empty() && (--s.end())->first == 3) {
    while (!s.empty())
      if (int((s).size()) == 1) {
        int num = s.begin()->second;
        s.erase(s.begin());
        while (!cyc[num].empty()) {
          int cur = cyc[num].front();
          cyc[num].pop();
          ans1[szans].push_back(cur);
          ans2[szans].push_back(p[cur]);
        }
        szans++;
      } else if (s.begin()->first == 2 && (--s.end())->first == 3) {
        int num = s.begin()->second;
        s.erase(s.begin());
        while (!cyc[num].empty()) {
          int cur = cyc[num].front();
          cyc[num].pop();
          ans1[szans].push_back(cur);
          ans2[szans].push_back(p[cur]);
        }
        num = (--s.end())->second;
        s.erase(--s.end());
        while (!cyc[num].empty()) {
          int cur = cyc[num].front();
          cyc[num].pop();
          ans1[szans].push_back(cur);
          ans2[szans].push_back(p[cur]);
        }
        szans++;
      } else {
        int num = s.begin()->second;
        s.erase(s.begin());
        while (!cyc[num].empty()) {
          int cur = cyc[num].front();
          cyc[num].pop();
          ans1[szans].push_back(cur);
          ans2[szans].push_back(p[cur]);
        }
        num = s.begin()->second;
        s.erase(s.begin());
        int cur1 = cyc[num].front();
        cyc[num].pop();
        int cur2 = cyc[num].front();
        cyc[num].pop();
        ans1[szans].push_back(cur1);
        ans1[szans].push_back(cur2);
        ans2[szans].push_back(cur2);
        ans2[szans].push_back(cur1);
        p[cur1] = p[cur2];
        cyc[num].push(cur1);
        s.insert(make_pair((int((cyc[num]).size())), (num)));
        szans++;
      }
  }
  printf("%d\n", szans);
  for (int i = 0; i < int(szans); i++) {
    printf("%d\n", int((ans1[i]).size()));
    for (int j = 0; j < int(int((ans1[i]).size())); j++)
      printf("%d ", ans1[i][j] + 1);
    puts("");
    for (int j = 0; j < int(int((ans2[i]).size())); j++)
      printf("%d ", ans2[i][j] + 1);
    puts("");
  }
  return 0;
}
