#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
  return x * x;
}
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &v[i]);
    --v[i];
  }
  vector<pair<int, int> > per;
  vector<vector<pair<int, int> > > p[4];
  for (int i = 0; i < ((int)(v).size()); ++i) {
    if (v[i] == -1) continue;
    vector<int> c;
    int cur = i;
    while (v[cur] != -1) {
      c.push_back(cur);
      int nex = v[cur];
      v[cur] = -1;
      cur = nex;
    }
    if (((int)(c).size()) <= 1) continue;
    vector<pair<int, int> > vc;
    for (int j = 0; j < ((int)(c).size()); ++j) {
      int k = (j + 1) % ((int)(c).size());
      vc.push_back(pair<int, int>(c[j], c[k]));
    }
    p[((int)(vc).size()) % 4].push_back(vc);
  }
  while (((int)(p[1]).size())) {
    vector<pair<int, int> > w = p[1][((int)(p[1]).size()) - 1];
    for (int i = 0; i < ((int)(w).size()); ++i) per.push_back(w[i]);
    p[1].pop_back();
  }
  while (((int)(p[0]).size())) {
    vector<pair<int, int> > w = p[0][((int)(p[0]).size()) - 1];
    for (int i = 0; i < ((int)(w).size()); ++i) per.push_back(w[i]);
    p[0].pop_back();
  }
  while (((int)(p[2]).size()) && ((int)(p[3]).size())) {
    vector<pair<int, int> > w = p[2][((int)(p[2]).size()) - 1];
    for (int i = 0; i < ((int)(w).size()); ++i) per.push_back(w[i]);
    p[2].pop_back();
    w = p[3][((int)(p[3]).size()) - 1];
    for (int i = 0; i < ((int)(w).size()); ++i) per.push_back(w[i]);
    p[3].pop_back();
  }
  while (((int)(p[2]).size())) {
    vector<pair<int, int> > w = p[2][((int)(p[2]).size()) - 1];
    for (int i = 0; i < ((int)(w).size()); ++i) per.push_back(w[i]);
    p[2].pop_back();
  }
  while (((int)(p[3]).size())) {
    vector<pair<int, int> > w = p[3][((int)(p[3]).size()) - 1];
    for (int i = 0; i < ((int)(w).size()); ++i) per.push_back(w[i]);
    p[3].pop_back();
  }
  vector<vector<pair<int, int> > > res;
  for (int i = 0; i < ((int)(per).size()); i += 4) {
    vector<pair<int, int> > cur;
    set<int> s;
    for (int j = 0; j < 5 && i + j < ((int)(per).size()); ++j) {
      cur.push_back(per[i + j]);
      s.insert(per[i + j].first);
    }
    for (int j = 0; j < 5 && i + j < ((int)(per).size()); ++j) {
      if (s.count(per[i + j].second)) s.erase(per[i + j].second);
    }
    if (!((int)(s).size())) {
      res.push_back(cur);
      ++i;
      continue;
    }
    int x = *s.begin();
    cur[((int)(cur).size()) - 1].second = x;
    res.push_back(cur);
    per[i + 4].first = x;
  }
  printf("%d\n", ((int)(res).size()));
  for (int i = 0; i < ((int)(res).size()); ++i) {
    printf("%d\n", ((int)(res[i]).size()));
    for (int j = 0; j < ((int)(res[i]).size()); ++j)
      printf("%d ", res[i][j].first + 1);
    printf("\n");
    for (int j = 0; j < ((int)(res[i]).size()); ++j)
      printf("%d ", res[i][j].second + 1);
    printf("\n");
  }
  return 0;
}
