#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 9;
const double error = 1e-7;
const double PI = acos(-1);
mt19937 rng(
    (unsigned int)chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
  long long int y = x % m;
  return (y >= 0) ? y : y + m;
}
const int inf = 1e9 + 2;
const long long int infl = 1e18 + 1;
const int nmax = 1e5 + 10;
int input[nmax];
bool visited[nmax];
int output[nmax][2][5];
void print(int ans, int x, vector<int> &v, int k) {
  for (int i = x, j = v.size() - k; j < v.size(); i++, j++)
    output[ans][0][i] = v[j];
  for (int i = x, j = v.size() - k + 1; j < v.size(); i++, j++)
    output[ans][1][i] = v[j];
  output[ans][1][x + k - 1] = v[v.size() - k];
}
vector<vector<int> > newcycles[5];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> input[i];
  vector<vector<int> > cycles;
  for (int i = 1; i <= n; i++) {
    if (visited[i]) continue;
    cycles.push_back(vector<int>(0));
    while (!visited[i]) {
      visited[i] = true;
      cycles.back().push_back(i);
      i = input[i];
    }
  }
  sort(cycles.begin(), cycles.end(),
       [&](vector<int> &v, vector<int> &w) { return v.size() < w.size(); });
  int ans = 0;
  for (int i = cycles.size() - 1; i >= 0; i--) {
    auto &v = cycles[i];
    while (v.size() > 5) {
      print(ans, 0, v, 5);
      for (int j = 0; j < 4; j++) v.pop_back();
      ans++;
    }
    if (v.size() == 5) {
      print(ans, 0, v, 5);
      ans++;
    } else if (v.size() > 1)
      newcycles[v.size()].push_back(v);
  }
  while (newcycles[3].size() != 0) {
    auto v = newcycles[3].back();
    newcycles[3].pop_back();
    print(ans, 0, v, 3);
    if (newcycles[2].size() != 0) {
      print(ans, 3, newcycles[2].back(), 2);
      newcycles[2].pop_back();
    } else if (newcycles[4].size() != 0) {
      print(ans, 3, newcycles[4].back(), 2);
      newcycles[4].back().pop_back();
      newcycles[3].push_back(newcycles[4].back());
      newcycles[4].pop_back();
    } else if (newcycles[3].size() != 0) {
      print(ans, 3, newcycles[3].back(), 2);
      newcycles[3].back().pop_back();
      newcycles[2].push_back(newcycles[3].back());
      newcycles[3].pop_back();
    }
    ans++;
  }
  while (newcycles[2].size() != 0) {
    auto v = newcycles[2].back();
    newcycles[2].pop_back();
    print(ans, 0, v, 2);
    if (newcycles[3].size() != 0) {
      print(ans, 2, newcycles[3].back(), 3);
      newcycles[3].pop_back();
    } else if (newcycles[4].size() != 0) {
      print(ans, 2, newcycles[4].back(), 3);
      newcycles[4].back().pop_back();
      newcycles[4].back().pop_back();
      newcycles[2].push_back(newcycles[4].back());
      newcycles[4].pop_back();
    } else if (newcycles[2].size() != 0) {
      print(ans, 2, newcycles[2].back(), 2);
      newcycles[2].pop_back();
    }
    ans++;
  }
  while (newcycles[4].size() != 0) {
    auto v = newcycles[4].back();
    newcycles[4].pop_back();
    print(ans, 0, v, 4);
    ans++;
  }
  cout << ans << "\n";
  for (int i = 0; i < ans; i++) {
    int x;
    for (x = 0; x < 5; x++)
      if (output[i][0][x] == 0) break;
    cout << x << "\n";
    for (int k = 0; k < x; k++) cout << output[i][0][k] << " ";
    cout << "\n";
    for (int k = 0; k < x; k++) cout << output[i][1][k] << " ";
    cout << "\n";
  }
}
