1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
#!python

n = int(input())

t = []
for i in range(n):
    t.append(int(input()))
tt=sorted(t)
m = {x: i for i, x in enumerate(tt)}
t = [m[x] for x in t]

#print([x+1 for x in t])
cycles = []
used = set()
for i in range(n):
    c = []
    curr = t[i]
    while curr not in used:
        c.append(curr)
        used.add(curr)
        curr = t[curr]
    if len(c) > 0:
        cycles.append(c)

cycles = [[x+1 for x in c] for c in cycles]
max_len = max([len(c) for c in cycles])
#print(cycles)

def solve2(cycles):
    #print(cycles)
    pref = []
    suf = []
    for c in cycles:
        if len(c) == 2:
            pref.append(c[0])
            suf.append(c[1])
    out = pref + suf[::-1]
    print(len(out))
    print(" ".join([str(i) for i in out]))

def solve_single_cycle(cycle):
    #print("solve_single", cycle)
    l = len(cycle)
    if l < 2:
        return [], []
    elif l == 2:
        return [cycle], []
    m = {}
    m_rev = {}
    for i, x in enumerate(cycle):
        to = cycle[(i+1) % len(cycle)]
        m[x] = to
        m_rev[to] = x
    #print(m)
    #print(m_rev)
    first = []
    second = []
    a, b, c = cycle[0], cycle[1], cycle[2]
    while len(set([a,b,c])) >= 3: 
        first.append([a, c])
        second.append([b, c])
        a, b, c = m_rev[a], a, m[c]
    if b != c:
        second.append([b, c])
    #print(first)
    #print(second)
    return first, second


if max_len == 1:
    print(0)
elif max_len == 2:
    print(1)
    solve2(cycles)
else:
    print(2)
    c1 = []
    c2 = []
    for c in cycles:
        pairs_1, pairs_2 = solve_single_cycle(c)
        c1.extend(pairs_1)
        c2.extend(pairs_2)
    solve2(c1)
    solve2(c2)
    
    """
    tx = [x for x in t]
    used = set()
    for c in c1:
        assert c[0] not in used
        assert c[1] not in used
        used.add(c[0])
        used.add(c[1])
        tx[c[0]-1], tx[c[1]-1] = tx[c[1]-1], tx[c[0]-1]

    used = set()
    for c in c2:
        assert c[0] not in used
        assert c[1] not in used
        used.add(c[0])
        used.add(c[1])
 
        tx[c[0]-1], tx[c[1]-1] = tx[c[1]-1], tx[c[0]-1]
    #print(tx)
    assert tx == list(range(n))
    """