using
System;
using
System.Collections.Generic;
public
class
GFG {
public
class
PairL {
public
long
first;
public
long
second;
public
PairL(
long
f,
long
s)
{
first = f;
second = s;
}
}
public
class
Pair {
public
long
first;
public
PairL second;
public
Pair(
long
f, PairL s)
{
first = f;
second = s;
}
}
static
void
sortPermutation(
long
[] arr,
long
n)
{
List<Pair> ans =
new
List<Pair>();
List<
long
> p =
new
List<
long
>();
bool
[] visited =
new
bool
[200005];
for
(
int
i = 1; i <= n; i++) {
if
(arr[i] == i) {
visited[i] =
true
;
continue
;
}
else
{
if
(!visited[i]) {
long
x = i;
List<
long
> v =
new
List<
long
>();
while
(!visited[(
int
)x]) {
visited[(
int
)x] =
true
;
v.Add(x);
x = arr[(
int
)x];
}
if
((v.Count - 3) % 2 == 0) {
for
(
int
j = 1; j < v.Count;
j += 2) {
ans.Add(
new
Pair(
v[0],
new
PairL(v[j], v[j + 1])));
}
continue
;
}
p.Add(v[0]);
p.Add(v[v.Count - 1]);
for
(
int
j = 1; j < v.Count - 1;
j += 2) {
ans.Add(
new
Pair(
v[0],
new
PairL(v[j], v[j + 1])));
}
}
}
}
if
(p.Count % 4 == 1) {
Console.WriteLine(
"-1"
);
return
;
}
for
(
int
i = 0; i < p.Count; i += 4) {
ans.Add(
new
Pair(
p[i],
new
PairL(p[i + 1], p[i + 2])));
ans.Add(
new
Pair(p[i + 2],
new
PairL(p[i], p[i + 3])));
}
Console.WriteLine(ans.Count);
for
(
int
i = 0; i < ans.Count; i++) {
Console.WriteLine(ans[i].first +
" "
+ ans[i].second.first +
" "
+ ans[i].second.second);
}
}
static
public
void
Main()
{
long
[] arr = { 0, 3, 2, 4, 1 };
long
n = 4;
sortPermutation(arr, n);
}
}