A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not.

A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a_0, a_1, ..., a_{n} - 1 if and only if a_{i} = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points.

You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 10^5). The second line contains n integers a_0, a_1, ..., a_{n} - 1 — the given permutation.


-----Output-----

Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.


-----Examples-----
Input
5
0 1 3 4 2

Output
3

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation

Complete search


Solutions:
n = int(input())
a = list(map(int, input().split()))
b = 0
c = 0
for (i, j) in enumerate(a):
	if i == j:
		b += 1
	elif a[j] == i:
		c = 2
	elif c == 0:
		c = 1
print(b + c)


n = int(input())
a = list(map(int, input().split()))
c = 0
for i in range(n):
	if a[i] == i:
		c += 1
if c < n:
	for i in range(n):
		if a[i] != i and a[a[i]] == i:
			c += 2
			break
	else:
		c += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
k = 0
m = 0
for i in range(n):
	if i == a[i]:
		k += 1
	elif i == a[a[i]]:
		m = 1
print(min(k + 1 + m, n))


a = int(input())
x = list(map(int, input().split()))
fixed = 0
y = [-1 for i in range(a)]
for i in range(a):
	if x[i] == i:
		fixed += 1
	else:
		y[x[i]] = i
for i in range(a):
	if x[i] != i:
		if x[i] == y[i]:
			print(fixed + 2)
			quit()
else:
	print(min(a, fixed + 1))


n = int(input())
a = [int(i) for i in input().split()]
intl = 0
for i in range(n):
	if i == a[i]:
		intl += 1
pzbl = 0
for i in range(n):
	if i != a[i]:
		if i == a[a[i]]:
			pzbl = 2
		else:
			pzbl = max(pzbl, 1)
print(intl + pzbl)


n = int(input())
arr = [int(x) for x in input().split(' ')]
(paired, fixpoints) = (0, 0)
for x in range(n):
	if arr[x] == x:
		fixpoints += 1
	elif arr[arr[x]] == x:
		paired += 1
if fixpoints == n:
	print(fixpoints)
elif paired > 0:
	print(fixpoints + 2)
else:
	print(fixpoints + 1)


n = int(input())
arr = [int(i) for i in input().split()]
ans = 0
boo = True
for i in range(n):
	if arr[i] != i:
		if arr[arr[i]] == i:
			arr[arr[i]] = arr[i]
			arr[i] = i
			boo = False
			break
for i in range(n):
	if arr[i] == i:
		ans += 1
if ans != n:
	if boo:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(n):
	if a[i] == i:
		count += 1
for i in range(0, n):
	val = a[i]
	if a[val] == i and val != i:
		print(count + 2)
		exit()
if count == n:
	print(n)
else:
	print(count + 1)


n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(n):
	if a[i] == i:
		res += 1
for i in range(n):
	if a[i] != i and a[a[i]] == i:
		res += 2
		print(res)
		return
if res != n:
	res += 1
print(res)


n = int(input())
lst = list(map(int, input().split(' ')))
maxed = 0
c = 0
maps = {}
for i in range(len(lst)):
	maps[lst[i]] = i
for i in range(len(lst)):
	if lst[i] == i:
		continue
	if c > 0:
		break
	if lst[i] == maps[i]:
		temp = lst[i]
		lst[i] = i
		lst[maps[i]] = temp
		c += 1
	elif c == 0 and i == len(lst) - 1:
		temp = lst[i]
		lst[i] = i
for i in range(len(lst)):
	if i == lst[i]:
		maxed += 1
print(maxed)


n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(n):
	if a[i] == i:
		ans += 1
if ans == n:
	print(ans)
	exit()
for i in range(n):
	if a[i] != i:
		if a[a[i]] == i:
			print(ans + 2)
			break
else:
	print(ans + 1)


looping = int(input())
setofnum = list(map(int, input().split()))
total = 0
maxnum = 0
for i in range(looping):
	if i == setofnum[i]:
		total += 1
	elif maxnum < 2 and i > setofnum[i]:
		if setofnum[setofnum[i]] == i:
			maxnum = 2
		else:
			maxnum = 1
print(total + maxnum)


n = int(input())
s = input()
l = s.split()
p = 0
verif = True
l = [int(l[i]) for i in range(len(l))]
for i in range(len(l)):
	if i == l[i]:
		p = p + 1
	elif l[l[i]] == i and verif == True:
		verif = False
		p = p + 2
if verif == True and p < n:
	p = p + 1
print(p)


n = int(input())
lst = list(map(int, input().split()))
good = len([i for i in range(n) if lst[i] == i])
best = len([i for i in range(n) if i != lst[i] and lst[lst[i]] == i])
var = len([i for i in range(n) if i != lst[i] and lst[lst[i]] != lst[i]])
print(good + (2 if best else var > 0))


n = int(input())
arr = [int(i) for i in input().split()]
d = {}
equal = 0
swap = 1
for i in range(n):
	if i == arr[i]:
		equal += 1
	elif i == arr[arr[i]]:
		swap = 2
if equal == n:
	print(n)
else:
	print(equal + swap)


n = int(input())
(*a,) = map(int, input().split())
c = 0
sw = 0
for i in range(n):
	if i == a[i]:
		c += 1
	elif i == a[a[i]]:
		sw = 1
print([c + 2 * sw, c + 1][sw == 0 and c != n])


n = int(input())
a = list(map(int, input().split()))
c = 0
ans = 0
for i in range(n):
	if a[i] == i:
		ans += 1
	elif c < 2:
		if i == a[a[i]]:
			c = 2
		else:
			c = 1
print(ans + c)


n = int(input())
a = [int(i) for i in input().split()]
have = sum((a[x] == x for x in range(n)))
ans = have
for i in range(n):
	if i == a[i] or a[i] == a[a[i]]:
		continue
	ans = max(ans, have + 1)
	if a[a[i]] == i:
		ans = max(ans, have + 2)
print(ans)


(n, seq) = (int(input()), list(map(int, input().split())))
counter = found = 0
for i in range(n):
	if seq[i] == i:
		counter += 1
	else:
		found += seq[seq[i]] == i
print(counter + 2 * (found > 0) + (counter < n and (not found)))


n = int(input())
s = input()
A = list(map(int, s.split()))
(ret, sw, c) = (0, 0, 1)
for i in range(len(A)):
	if A[i] == i:
		ret += 1
		continue
	sw += 1
	if A[A[i]] == i:
		c = 2
if sw >= 2:
	ret = ret + c
print(ret)


n = int(input())
a = list(map(int, input().split()))
c = 0
t = 0
for i in range(n):
	if a[i] == i:
		c += 1
	elif i == a[a[i]]:
		t = 1
print(c + t + int(c != n))


class CodeforcesTask347BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.permutation = []

	def read_input(self):
		self.n = int(input())
		self.permutation = [int(x) for x in input().split(' ')]

	def process_task(self):
		fixed_points = sum([1 if self.permutation[x] == x else 0 for x in range(self.n)])
		if fixed_points == self.n:
			self.result = str(self.n)
		else:
			double = False
			for x in range(self.n):
				if self.permutation[x] != x:
					if self.permutation[self.permutation[x]] == x:
						double = True
						break
			if double:
				fixed_points += 2
			else:
				fixed_points += 1
			self.result = str(fixed_points)

	def get_result(self):
		return self.result
Solution = CodeforcesTask347BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


def permutations(n, arr):
	count = 0
	flag = False
	for i in range(n):
		if i == arr[i]:
			count += 1
		elif i == arr[arr[i]]:
			flag = True
	if count == n:
		print(count)
		return
	elif flag:
		count += 2
	else:
		count += 1
	print(count)
n = int(input())
l = list(map(int, input().split()))
permutations(n, l)


def main():
	n = int(input())
	a = list(map(int, input().split(' ')))
	k = []
	f = 0
	for (i, ai) in enumerate(a):
		if ai != i:
			k.append((i, ai))
		else:
			f += 1
	k = sorted(k, key=lambda x: x[0])
	m = set()
	x = 1 if len(k) > 0 else 0
	for (i, ai) in k:
		if (i, ai) in m:
			x = 2
			break
		m.add((i, ai))
		m.add((ai, i))
	return f + x
print(main())


from sys import stdin

def max_nb_fixed_points(n, permutation):
	pos_for_val = {}
	initial_fixed = 0
	double_swap = False
	single_swap = False
	for i in range(n):
		if i == permutation[i]:
			initial_fixed += 1
			continue
		if not double_swap:
			if i in pos_for_val:
				j = pos_for_val[i]
				if permutation[i] == j:
					double_swap = True
				else:
					single_swap = True
			pos_for_val[permutation[i]] = i
	if double_swap:
		return initial_fixed + 2
	if single_swap:
		return initial_fixed + 1
	return initial_fixed
n = int(stdin.readline())
permutation = [int(c) for c in stdin.readline().split(' ')]
print(max_nb_fixed_points(n, permutation))


swap = False
n = int(input())
ris = 0
pos = [n + 1] * n
perm = list(map(int, input().split()))
for cont in range(0, n):
	if perm[cont] == cont:
		ris += 1
	else:
		pos[perm[cont]] = cont
		if swap is False:
			if pos[cont] == perm[cont]:
				ris += 2
				swap = True
if swap is False:
	if ris < n:
		ris += 1
print(ris)


from math import sqrt
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def prog_name():
	n = int(input())
	l = list(map(int, input().split()))
	cnt = c = 0
	for x in range(len(l)):
		if x == l[x]:
			cnt += 1
		elif x == l[l[x]] and c == 0:
			cnt += 2
			c += 1
	if cnt == n:
		print(n)
	else:
		print(cnt if c > 0 else cnt + 1)

def main():
	t = 1
	for unique in range(t):
		prog_name()
main()


def main():
	n = int(input())
	aa = list(map(int, input().split()))
	res = sum((i == x for (i, x) in enumerate(aa)))
	if res > n - 3:
		res = n
	else:
		for (i, x) in enumerate(aa):
			if aa[x] == i != x:
				res += 2
				break
		else:
			res += 1
	print(res)
main()


n = int(input())
a = list(map(int, input().split()))
ans = 0
ans1 = 0
for i in range(len(a)):
	if a[i] == i:
		ans += 1
		ans1 += 1
if ans != n:
	for i in range(len(a)):
		if a[i] != i:
			if a[a[i]] == i:
				ans += 2
				break
	if ans == ans1:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
c = 0
t = False
for i in range(len(a)):
	if a[i] == i:
		c += 1
	elif a[a[i]] == i and t == False:
		t = True
if n == c or n - 1 == c:
	print(c)
	return
if t == True:
	print(c + 2)
else:
	print(c + 1)


from sys import stdin, stdout
import math
n = int(stdin.readline())
li = list(map(int, stdin.readline().split()))
ans = 0
f = 0
for i in range(0, n):
	if li[i] == i:
		ans += 1
	else:
		x = li[i]
		if li[x] == i:
			f = 1
if ans == n:
	print(ans)
elif f == 1:
	print(ans + 2)
else:
	print(ans + 1)


n = int(input())
p = [int(x) for x in input().split()]
if n <= 2:
	print(n)
else:
	s = 0
	t = 0
	for i in range(0, n):
		if p[i] == i:
			s += 1
	if s == n or s == n - 2:
		print(n)
	else:
		for i in range(0, n):
			if p[i] != i and p[p[i]] == i:
				s += 2
				t = 1
				break
		if t == 0:
			s += 1
		print(s)


n = int(input())
lis = list(map(int, input().split()))
lis2 = [0] * n
res = 0
for i in range(n):
	lis2[i] = lis[i] - i
	if lis2[i] == 0:
		res += 1
mset = set([])
fnd = 0
for i in lis:
	if lis2[i] != 0:
		if i == lis[lis[i]] and i != lis[i]:
			fnd = 1
			break
if fnd:
	res += 2
elif res != n and res != n - 1:
	res += 1
print(res)


N = int(input())
i = 0
j = 0
num = []
new = []
k = 0
total = 0
isSorted = False
flagDouble = False
IN = input().split()
while i < N:
	k = int(IN[i])
	num.append((k, i))
	new.append(k)
	if i == new[i]:
		total = total + 1
	i = i + 1
if total == N:
	isSorted = True
else:
	num.sort()
for i in range(N):
	j = num[i][1]
	if i == new[j] and j == new[i] and (i != j):
		flagDouble = True
if flagDouble:
	print(total + 2)
elif not isSorted:
	print(total + 1)
else:
	print(total)


import sys
from collections import Counter
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
N = inp()
L = inlt()
out = 0
fl = True
for n in range(N):
	if n == L[n]:
		out += 1
	elif fl and L[L[n]] == n:
		out += 2
		fl = False
if fl and out < N:
	out += 1
print(out)


n = int(input())
l = list(map(int, input().split(' ')))
certos = 0
achou_2 = False
errados = 0
for i in range(len(l)):
	if l[i] == i:
		certos += 1
	else:
		errados += 1
		if l[l[i]] == i:
			achou_2 = True
if errados > 1:
	if achou_2:
		certos += 2
	else:
		certos += 1
print(certos)


n = int(input())
inpt = list(map(int, input().split(' ')))
count = 0
b = 0
for i in range(n):
	if inpt[i] == i:
		count += 1
	if b == 0 and inpt[i] != i and (inpt[inpt[i]] == i):
		count += 2
		b = 1
if b == 0:
	count += 1
print(min(n, count))


mod = 10 ** 9 + 7
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n = ii()
l = il()
flg = 0
c = 0
for (i, j) in enumerate(l):
	if i == j:
		c += 1
	elif l[j] == i:
		flg = 1
print(c if c == n else [c + 1, c + 2][flg == 1])


n = int(input())
a = list(map(int, input().split()))
(o, ot) = (0, 0)
for i in range(n):
	if a[i] == i:
		o += 1
for i in range(n):
	if a[i] != i:
		if i == a[a[i]]:
			ot = 2
		else:
			ot = max(ot, 1)
print(ot + o)


def main():
	n = map(int, input().split())
	a = list(map(int, input().split()))
	cnt = 0
	for i in range(len(a)):
		if a[i] == i:
			cnt += 1
	if cnt == len(a):
		print(cnt)
		return
	for i in range(len(a)):
		if a[i] != i:
			if i == a[a[i]]:
				print(cnt + 2)
				return
	print(cnt + 1)
	return
main()


n = int(input())
a = list(map(int, input().split()))
ctr = 0
foundPair = False
for i in range(n):
	if a[i] == i:
		ctr += 1
	else:
		j = a[i]
		if a[j] == i:
			foundPair = True
if ctr == n:
	print(ctr)
elif foundPair:
	print(ctr + 2)
else:
	print(ctr + 1)


n = int(input())
a = list(map(int, input().split()))
ans = 0
b = True
for i in range(n):
	if a[i] == i:
		ans += 1
	elif a[a[i]] == i and b:
		ans += 2
		b = False
if b and ans < n:
	ans += 1
print(ans)


n = int(input())
tab = list(map(int, input().split(' ')))
b = False
cnt = 0
for i in range(n):
	if tab[i] == i:
		cnt += 1
	elif tab[tab[i]] == i:
		b = True
if cnt == n:
	print(n)
elif b:
	print(cnt + 2)
else:
	print(cnt + 1)


n = int(input())
l = list(map(int, input().split()))
c = 0
f = 1
for i in range(n):
	if l[i] == i:
		c = c + 1
	elif f == 1 and l[l[i]] == i:
		c = c + 2
		f = 0
if f == 1 and c < n:
	c = c + 1
print(c)


n = int(input())
a = [int(i) for i in input().split()]
(ans, t) = (0, 0)
for i in range(n):
	if i == a[i]:
		ans += 1
	elif i == a[a[i]]:
		t = 1
print(min(n, ans + t + 1))


n = int(input())
li = list(map(int, input().split(' ')))
indexes = [0] * n
i = 0
while i < len(li):
	indexes[li[i]] = i
	i += 1
swapped = False
allequal = True
i = 0
count = 0
while i < len(li):
	if li[i] == i:
		count += 1
	else:
		allequal = False
		if swapped == False:
			if indexes[i] == li[i]:
				count += 2
				swapped = True
	i += 1
if swapped == False and allequal == False:
	count += 1
print(count)


n = int(input())
data = list(map(int, input().split()))
places = dict()
i = 0
count = 0
check = True
for num in data:
	if num == i:
		count += 1
	elif check:
		if num in places:
			if places[num] == i:
				count += 2
				check = False
			else:
				places[i] = num
		else:
			places[i] = num
	i += 1
if check and count + 1 <= n:
	print(count + 1)
else:
	print(count)


def __starting_point():
	inp = input()
	inp = input()
	arr = inp.split()
	ans = 0
	ext = 0
	for i in range(len(arr)):
		if i == int(arr[i]):
			ans += 1
		elif int(arr[int(arr[i])]) == i:
			ext = max(ext, 2)
		else:
			ext = max(ext, 1)
	ans += ext
	print(ans)
__starting_point()


import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def main():
	try:
		n = inp()
		a = inlt()
		ans = 0
		f = False
		for i in range(n):
			if a[i] == i:
				ans += 1
			elif not f and a[a[i]] == i:
				ans += 2
				f = True
		if n > ans and (not f):
			print(ans + 1)
		else:
			print(ans)
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import *
from collections import *

def inp():
	return map(int, input().split())

def inp_arr():
	return list(map(int, input().split()))

def lcm(a, b):
	return a * b / gcd(a, b)
n = int(input())
arr = inp_arr()
c = 0
flag = 0
for i in range(len(arr)):
	if i == arr[i]:
		c += 1
		if i == len(arr) - 1 and flag == 0:
			flag = 1
	elif arr[arr[i]] == i and flag == 0:
		c += 2
		flag = 1
print(c + 1 if flag == 0 else c)


n = int(input())
arr = list(map(int, input().split()))
cnt = check = 0
flag = True
for i in range(n):
	if arr[i] == i:
		cnt += 1
	elif flag:
		check = 1
		if arr[arr[i]] == i:
			cnt += 2
			flag = False
if flag and check == 1:
	print(cnt + 1)
else:
	print(cnt)


n = int(input())
perm = list(map(int, input().split()))
fixed = 0
swap = 0
for i in range(n):
	if i == perm[i]:
		fixed += 1
for i in range(n):
	if perm[i] != i:
		if perm[perm[i]] == i:
			fixed += 2
			swap = 1
			break
if swap == 0 and fixed != n:
	fixed += 1
print(fixed)


import sys
sys.setrecursionlimit(100000)
n = int(input())
arr = [int(x) for x in sys.stdin.readline().strip().split()]
count = 0
flag = False
for i in range(0, n):
	if arr[i] == i:
		count += 1
	elif arr[arr[i]] == i:
		flag = True
if count != n:
	print(count + 1 if not flag else count + 2)
else:
	print(count)


def main():
	n = int(input())
	v = list(map(int, input().split()))
	(a, c1, ok) = (0, 0, 0)
	for i in range(n):
		if v[i] == i:
			c1 = c1 + 1
		elif v[v[i]] == i:
			ok = 1
	if c1 == n:
		print(c1)
	elif ok:
		print(c1 + 2)
	else:
		print(c1 + 1)
main()


x = int(input())
y = [int(p) for p in input().split()]
temp = 0
b = 0
c = 0
for (ind, val) in enumerate(y):
	if val == ind:
		b += 1
	elif y[val] == ind:
		c = 2
	elif c == 0:
		c = 1
print(b + c)


n = int(input())
A = list(map(int, input().split()))
_counter = 0
_c2 = 0
B = []
for (i, a) in enumerate(A):
	if i == a:
		_counter += 1
	else:
		B.append([a, i])
i = 0
while i < len(B) and _c2 != 2:
	if B[i][0] < n and B[i][1] < n:
		if A[B[i][0]] == B[i][1]:
			_c2 = 2
		else:
			_c2 = 1
	i += 1
print(_counter + _c2)


n = int(input())
line = list(map(int, input().split()))
ret = flag = 0
for i in range(n):
	if i == line[i]:
		ret += 1
if ret == n:
	print(ret)
else:
	for i in range(n):
		if i != line[i] and i == line[line[i]]:
			flag = 1
	print(ret + 1 + flag)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return map(int, input().split())

def array():
	return [int(i) for i in input().split()]
n = int(input())
a = array()
d = {a[i]: i for i in a}
f = 0
c = 0
for i in range(n):
	if i == a[i]:
		c += 1
if c == n:
	print(c)
	exit()
for i in range(n):
	if i != a[i] and a[a[i]] == i:
		print(c + 2)
		exit()
print(c + 1)


n = int(input())
l = list(map(int, input().split()))
(k, t) = (0, 0)
for i in range(n):
	if i == l[i]:
		k += 1
	elif i == l[l[i]]:
		t = 1
if t == 1:
	print(min(n, k + 2))
else:
	print(min(n, k + 1))


import os
import sys
from math import ceil, gcd, sqrt, log2, floor
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	n = int(input())
	a = list(map(int, input().split()))
	count = 0
	flag = False
	if a == list(range(n)):
		print(n)
	else:
		for i in range(n):
			if a[i] == i:
				count += 1
			elif a[a[i]] == i:
				flag = True
		if flag == True:
			print(count + 2)
		else:
			print(count + 1)
main()


n = int(input())
l = list(map(int, input().split()))
count = 0
d = {}
for i in range(len(l)):
	if l[i] == i:
		count = count + 1
	else:
		d[i] = l[i]
for i in d:
	if d[i] in d:
		if d[d[i]] == i:
			print(count + 2)
			exit()
			break
if len(d):
	print(count + 1)
else:
	print(count)


def f(l):
	n = len(l)
	c = sum([i == l[i] for i in range(n)])
	if c == n:
		return c
	d = sum([l[l[i]] == i for i in range(n) if i != l[i]])
	return c + 2 if d > 0 else c + 1
q = int(input())
l = list(map(int, input().split()))
print(f(l))


n = int(input())
l = list(map(int, input().split()))
c = 0
t = 0
for i in range(n):
	if l[i] == i:
		c += 1
	elif l[i] != i and l[l[i]] == i:
		t += 1
	else:
		pass
if c != n:
	if t > 0:
		print(c + 2)
	else:
		print(c + 1)
else:
	print(c)


import sys
from bisect import bisect_left, bisect_right
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log10
from itertools import permutations
inf = float('inf')
mod = 1000000007
mini = 1000000007

def fact(n):
	if n == 0:
		return 1
	p = 1
	d = 10 ** 9 + 7
	for i in range(1, n + 1):
		p = p * i
		p = p % d
	return p

def ncr(n, r):
	d = 10 ** 9 + 7
	num = fact(n)
	den = fact(r) * fact(n - r) % d
	den = pow(den, d - 2, d)
	return num * den % d

def sieve(n):
	prime = [True for i in range(n + 1)]
	lst = []
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p = p + 1
	for i in range(2, n + 1):
		if prime[i]:
			lst.append(i)
	return lst

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def calculate_factors(n):
	hh = [1] * (n + 1)
	p = 2
	while p * p < n:
		if hh[p] == 1:
			for i in range(p * 2, n, p):
				hh[i] = 0
		p += 1
	total = 1
	for p in range(2, n + 1):
		if hh[p] == 1:
			count = 0
			if n % p == 0:
				while n % p == 0:
					n = int(n / p)
					count += 1
				total *= count + 1
	return total

def prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			factors.add(n // i)
			n = n // i
			factors.add(i)
	if n > 1:
		factors.add(n)
	return factors

def isPrime(n):
	if n == 2 or n == 3:
		return True
	if n % 2 == 0 or n < 2:
		return False
	for i in range(3, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True

def solve(n):
	if n == 1:
		return 1
	p = 2
	while p * p <= n:
		if n % p == 0:
			return n // p
		p = p + 1
	return False

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
Arr = get_array()
count_gud = 0
myset = set()
for i in range(n):
	if Arr[i] == i:
		count_gud = count_gud + 1
		myset.add(i)
count_bad = n - count_gud
flag = 0
for i in range(n - 1):
	if i not in myset:
		if i == Arr[Arr[i]]:
			flag = 1
			break
if flag == 1:
	print(count_gud + 2)
elif flag == 0 and count_gud == n:
	print(n)
else:
	print(count_gud + 1)


n = int(input())
a = [int(i) for i in input().split()]
s = 0
t = 0
for i in range(n):
	if i == a[i]:
		s = s + 1
	elif a[a[i]] == i:
		t = 1
print(s + (s < n) * (1 + t))


n = int(input())
a = list(map(int, input().split()))
c = 0
index = []
value = []
dict = {}
for i in range(n):
	if i == a[i]:
		c += 1
	else:
		index.append(i)
		value.append(a[i])
		dict[i] = a[i]
f = 0
for i in range(len(index)):
	idx = index[i]
	v = value[i]
	fd = dict.get(v)
	if fd == idx:
		c += 2
		f = 1
		print(c)
		break
if f == 0 and c < n:
	print(c + 1)
elif f == 0 and c == n:
	print(c)


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	n = int(input.readline())
	lis = list(map(int, input.readline().split()))
	count = 0
	for (i, x) in enumerate(lis):
		if x == i:
			count += 1
	gotit = False
	for (i, x) in enumerate(lis):
		if x != i:
			if lis[x] == i:
				count += 2
				gotit = True
				break
	if gotit:
		output = count
	elif count < n:
		output = count + 1
	else:
		output = count
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


n = int(input())
k = list(map(int, input().split(' ')))
c = 1
r = 0
for i in range(len(k)):
	if k[i] == i:
		c += 1
	elif k[k[i]] == i and r == 0:
		r += 1
print(c + r) if c < len(k) else print(c + r - 1)


n = int(input())
a = list(map(int, input().split()))
marked = [False] * n
(r, flag) = (0, False)
for i in range(n):
	if marked[i]:
		continue
	if a[i] == i:
		r += 1
		marked[i] = True
		continue
	else:
		(v, count, index, value) = ([], 0, i, -1)
		while value != i:
			marked[index] = True
			v.append(index)
			value = a[index]
			index = value
		if len(v) == 2:
			flag = True
if r == n:
	print(r)
elif flag:
	print(r + 2)
else:
	print(r + 1)


n = int(input())
a = list(map(int, input().split()))
ans = 0
k = 0
for i in range(n):
	if i == a[i]:
		ans += 1
	elif a[a[i]] == i and k != 2:
		ans += 2
		if k == 1:
			ans -= 1
		k = 2
	elif k == 0:
		ans += 1
		k = 1
print(ans)


n = int(input())
ai = list(map(int, input().split()))
cnt = 0
cnt1 = 0
for i in range(0, len(ai)):
	if ai[i] == i:
		cnt1 += 1
		cnt += 1
if cnt != n:
	for i in range(n):
		if ai[i] != i:
			if ai[ai[i]] == i:
				cnt += 2
				break
	if cnt == cnt1:
		cnt += 1
print(cnt)


n = int(input())
a = []
inp = input()
a = [int(x) for x in inp.split(' ')]
result = 0
swaped = False
flag = 0
all = 0
for i in range(len(a)):
	if a[i] > n - 1:
		flag += 1
		continue
	if i == a[i]:
		result += 1
		all += 1
		continue
	if a[a[i]] == i:
		swaped = True
if swaped:
	result += 2
elif flag == n and all == n:
	pass
elif n - result == flag:
	pass
else:
	result += 1
print(result)


n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for (i, x) in zip(range(n), a):
	if i == x:
		ans += 1
if ans + 2 >= n:
	print(n)
	exit(0)
for (i, x) in zip(range(n), a):
	if i != x and a[x] == i:
		print(ans + 2)
		exit(0)
print(ans + 1)


n = int(input())
a = list(map(int, input().split()))
cnt = 0
dict = {}
for i in range(n):
	if a[i] == i:
		cnt += 1
	else:
		dict[a[i]] = 1
for i in range(n):
	if a[i] != i and a[i] < n:
		if a[a[i]] == i:
			print(cnt + 2)
			exit(0)
for i in range(n):
	if a[i] != i:
		if dict[i] == 1:
			print(cnt + 1)
			exit(0)
print(cnt)


n = int(input())
a = list(map(int, input().split()))
(fix, diff) = (0, 0)
op = 0
for i in range(n):
	if a[i] == i:
		fix += 1
	elif a[a[i]] == i:
		if op == 0:
			fix += 2
			op += 1
if op == 0:
	if fix <= n - 2:
		fix += 1
print(fix)


def fixed_points(n, a):
	count = 0
	fixed = 0
	for (i, j) in enumerate(a):
		if i == j:
			fixed += 1
		elif a[j] == i:
			count = 2
		elif count == 0:
			count = 1
	return fixed + count
n = int(input())
a = list(map(int, input().split()))
result = fixed_points(n, a)
print(result)


from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
flag = True
b = a
for i in range(n):
	if a[i] != i and a[a[i]] == i:
		b = []
		for j in range(n):
			if j == i:
				b.append(a[a[i]])
			elif j == a[i]:
				b.append(a[i])
			else:
				b.append(a[j])
		flag = False
		break
if flag:
	for i in range(n):
		if a[i] != i:
			b = []
			for j in range(n):
				if j == i:
					b.append(a[a[i]])
				elif j == a[i]:
					b.append(a[i])
				else:
					b.append(a[j])
			break
ans = 0
for i in range(n):
	if b[i] == i:
		ans += 1
print(ans)


n = int(input())
A = list(map(int, input().split()))
count = 0
p = True
for i in range(n):
	if A[i] == i:
		count += 1
	elif p and A[A[i]] == i:
		count += 2
		p = False
if p and count < n - 1:
	count += 1
print(count)


y = int(input())
a = [int(x) for x in input().split()]
num = 0
j = 0
for i in range(y):
	if a[i] == i:
		num += 1
	elif a[a[i]] == i:
		j = 1
if num != y:
	print(num + j + 1)
else:
	print(num)


input()
a = list(map(int, input().split()))
b = 0
c = 0
for i in a:
	if a[i] == i:
		b += 1
	elif a[a[i]] == i:
		c += 1
print(b + 2 if c else min(b + 1, len(a)))


input()
x = y = 0
d = {}
for (i, a) in enumerate(map(int, input().split())):
	if a == i:
		x += 1
	else:
		d[i] = a
		if a in d and d[a] == i:
			y = 1
print(x + y + bool(d))


n = int(input())
a = list(map(int, input().split()))
fp = 0
flag = 0
for i in range(n):
	if i == a[i]:
		fp += 1
	elif flag == 0 and i == a[a[i]]:
		(a[a[i]], a[i]) = (a[i], a[a[i]])
		fp += 1
		flag = 1
if fp < n and flag == 0:
	fp += 1
print(fp)


__copyright__ = ''
__author__ = 'Son-Huy TRAN'
__email__ = 'sonhuytran@gmail.com'
__doc__ = ''
__version__ = '1.0'

def main() -> int:
	n = int(input())
	numbers = [int(word) for word in input().split()]
	count_fixed = 0
	has_swap = False
	for i in range(n):
		if i == numbers[i]:
			count_fixed += 1
		elif numbers[numbers[i]] == i:
			has_swap = True
	if count_fixed == n:
		print(count_fixed)
	elif has_swap:
		print(count_fixed + 2)
	else:
		print(count_fixed + 1)
	return 0
exit(main())


def main():
	n = int(input())
	list = [int(x) for x in input().split()]
	dic = {}
	count = 0
	two = False
	for (index, value) in enumerate(list):
		if index == value:
			count += 1
		if value in dic and dic[value] == index:
			two = True
		dic[index] = value
	if two:
		print(min(n, count + 2))
	else:
		print(min(n, count + 1))
main()


n = int(input())
p = list(map(int, input().split()))
f = 0
swap = False
for i in range(n):
	if p[i] == i:
		f += 1
for i in range(n):
	if p[i] != i:
		if i == p[p[i]]:
			swap = True
if swap:
	f = f + 2
elif f < n:
	f = f + 1
print(f)


n = int(input())
a = list(map(int, input().split()))
j = k = c = 0
b = []
for i in a:
	if j == i:
		k += 1
	else:
		b.append(i)
	j += 1
if len(b) > 0:
	for i in b:
		if i == a[a[i]]:
			print(k + 2)
			exit(0)
	print(k + 1)
else:
	print(k)


import sys
n = int(input())
a = list(map(int, input().split()))
ans = 0
par = True
for i in range(n):
	if i == a[i]:
		ans += 1
for i in range(n):
	if i != a[i] and i == a[a[i]]:
		ans += 2
		par = False
		break
if n != 1 and ans < n and par:
	ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
fixed = extra = 0
for i in range(n):
	if a[i] == i:
		fixed += 1
	elif a[a[i]] == i:
		extra = 2
	elif not extra:
		extra = 1
print(fixed + extra)


n = int(input())
sq = input().split()
l = []
q = []
count = 0
temp = 0
flag = 0
for i in range(0, len(sq)):
	val = int(sq[i])
	l.append(val)
	if i == val:
		count += 1
	else:
		q.append(i)
if len(q) == 0:
	print(count)
else:
	for j in q:
		if l[l[j]] == j:
			count += 2
			print(count)
			exit()
		else:
			temp = 1
	print(count + 1)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = int_array()
cnt = 0
for i in range(n):
	if a[i] == i:
		cnt += 1
if cnt == n:
	print(cnt)
	exit()
else:
	ans = cnt + 1
	f = 0
	for i in range(n):
		if a[i] != i:
			if a[a[i]] == i:
				f = 1
				break
	if f:
		ans += 1
	print(ans)


n = int(input())
c = 0
cc = 0
l = list(map(int, input().split()))
for i in range(n):
	if l[i] == i:
		c += 1
	elif i == l[l[i]]:
		cc = 1
print(c + cc + int(c != n))


n = int(input())
a = list(map(int, input().split()))
(s, t) = (0, 0)
for x in range(n):
	if a[x] == x:
		s += 1
	elif t == 0 and a[a[x]] == x:
		t = 1
print(s + t + int(s != n))


n = int(input())
l = list(map(int, input().split()))
count = 0
flag = 0
for k in range(n):
	if l[k] == k:
		count += 1
	elif flag == 0:
		if l[l[k]] == k:
			count += 2
			flag = 1
if flag == 0 and count < n:
	print(count + 1)
else:
	print(count)


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list or type(n) is str:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ''.join(map(str, arr))

def revn(n):
	return int(str(n)[::-1])

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True
n = ii()
a = li()
ans = 0
dk = {}
dv = {}
for i in range(n):
	if a[i] == i:
		ans += 1
	else:
		dv[i] = a[i]
		dk[a[i]] = i
add = False
for i in dk:
	if dk[i] == dv[i]:
		add = True
if add:
	print(ans + 2)
elif ans < n:
	print(ans + 1)
else:
	print(ans)


n = int(input())
dex = {}
tot = 0
for (i, e) in enumerate(map(int, input().split())):
	if i == e:
		tot += 1
	else:
		dex[i] = e
if tot == n:
	print(tot)
else:
	keys = dex.keys()
	for key in dex:
		if dex[key] in dex.keys():
			if key == dex[dex[key]]:
				print(tot + 2)
				exit()
	print(tot + 1)


n = int(input())
A = list(map(int, input().split()))
ans = 0
E = {}
for i in range(n):
	if A[i] == i:
		ans += 1
	else:
		E[i] = A[i]
done = False
for item in E:
	if E[E[item]] == item:
		ans += 2
		done = True
		break
if not done and len(E) > 0:
	ans += 1
print(ans)


i = int(input())
l = list(map(int, input().split()))
c = 0
che = {}
g = 0
p = 0
for x in range(i):
	if l[x] == x:
		c += 1
	else:
		p = 1
		if x in che:
			if che[x] == l[x]:
				g = 2
			else:
				che[l[x]] = x
		else:
			che[l[x]] = x
if p == 0:
	print(c)
else:
	print(c + 2 if g == 2 else c + 1)


n = int(input())
a = [int(num) for num in input().split()]
max = 0
flag = 0
for i in range(0, n):
	if a[i] == i:
		max = max + 1
	elif a[a[i]] == i and flag != 1:
		temp = a[i]
		a[i] = a[temp]
		a[temp] = temp
		max = max + 1
		flag = 1
	elif flag != 1:
		flag = 2
if flag == 2:
	max = max + 1
print(max)


n = int(input())
arr = list(map(int, input().split()))
c = 0
i = 0
while i < n:
	if arr[i] == i:
		c += 1
	i += 1
j = 0
flag = False
while j < n:
	temp = arr[j]
	if arr[temp] == j and temp != j:
		flag = True
		break
	j += 1
if flag == False:
	if c + 1 > n:
		print(n)
	else:
		print(c + 1)
elif flag == True:
	if c + 2 > n:
		print(n)
	else:
		print(c + 2)


n = int(input())
list1 = list(map(int, input().split()))
replacee = [-1] * n
cnt = 0
swap = False
for i in range(n):
	if list1[i] == i:
		cnt += 1
	else:
		replacee[i] = list1[i]
for i in range(n):
	if replacee[replacee[i]] == i and (not swap):
		swap = True
		cnt += 2
if swap:
	print(cnt)
elif replacee != [-1] * n:
	print(cnt + 1)
else:
	print(cnt)


input()
a = [int(x) for x in input().split()]
c = f = 0
for i in a:
	if a[i] == i:
		c = c + 1
	elif a[a[i]] == i:
		f = 1
ans = min(len(a), c + f + 1)
print(ans)


_ = input()
inp = list(map(int, input().split(' ')))

def countpoints(l: list) -> int:
	count = 0
	for (i, e) in enumerate(l):
		if i == e:
			count += 1
	return count
_m = countpoints(inp)
m = _m
for (i, e) in enumerate(inp):
	if i == e:
		continue
	if inp[e] == i:
		m = max(m, _m + 2)
		break
	else:
		m = max(m, _m + 1)
print(m)


n = int(input())
a = [int(i) for i in input().split()]
count = 0
add = 0
for i in range(n):
	if a[i] == i:
		count += 1
	elif a[a[i]] == i:
		add = 1
if count != n:
	add += 1
print(count + add)


n = int(input())
ls = input().split()
l = [int(i) for i in ls]
half = 0
full = 0
inplace = 0
for i in range(n):
	if l[i] != i:
		if l[l[i]] == i:
			full += 1
		else:
			half += 1
	else:
		inplace += 1
if inplace == n:
	print(n)
elif full > 0:
	print(inplace + 2)
else:
	print(inplace + 1)


n = int(input())
l = list(map(int, input().split()))
x = 0
for i in range(n):
	if l[i] == i:
		x += 1
flag = False
for i in range(n):
	if l[l[i]] == i and l[i] != i:
		flag = True
if flag:
	x += 2
else:
	x += 1
print(min(n, x))


n = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(n):
	cnt += A[i] == i
if cnt == n:
	print(n)
else:
	ind = False
	for i in range(n):
		ind |= A[i] != i and A[A[i]] == i
	if ind:
		print(cnt + 2)
	else:
		print(cnt + 1)


n = int(input())
l = [int(x) for x in input().split()]
fp = sw = 0
for (i, a) in enumerate(l):
	if i == a:
		fp += 1
	elif l[a] == i:
		sw = 2
	elif sw == 0:
		sw = 1
print(fp + sw)


import sys
input = sys.stdin.readline
from math import ceil, floor, factorial

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = int_array()
ans = 0
for i in range(n):
	if i == a[i]:
		ans += 1
multi = 0
single = 0
for i in range(n):
	if a[i] != i:
		if a[a[i]] == i:
			multi += 1
if multi > 0:
	ans += 2
elif ans != n:
	ans += 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
ans = 0
flag = True
for i in range(n):
	if a[i] == i:
		ans += 1
	elif a[a[i]] == i and flag:
		ans += 2
		flag = False
if flag and ans < n:
	ans += 1
print(ans)


n = int(input())
A = [*map(int, input().split())]
x = y = 0
for i in range(n):
	if A[i] == i:
		x += 1
	elif A[A[i]] == i:
		y = 1
print(x + y + (x != n))


n = int(input())
x = list(map(int, input().split()))
d = {}
ans = 0
for i in range(n):
	d[x[i]] = i
	if x[i] == i:
		ans += 1
if ans == n:
	print(ans)
	exit()
for j in range(n):
	if d[d[x[j]]] == x[j] and j != x[j]:
		ans += 2
		print(ans)
		exit()
print(ans + 1)


n = int(input())
arr = list(map(int, input().split()))
c = 0
d = 0
ar = [0] * n
for i in range(n):
	ar[arr[i]] = i
for i in range(n):
	if arr[i] == i:
		c += 1
	elif ar[i] == arr[i] and d == 0:
		d = 1
		c += 2
if c == n:
	print(n)
elif d == 0:
	print(c + 1)
else:
	print(c)


n = int(input())
b = []
count = 0
a = list(map(int, input().split()))
for i in range(n):
	if a[i] != i:
		b.append(i)
if len(b) > 0:
	for i in b:
		x = a[a[i]]
		if x == i:
			count += 1
			break
	if count > 0:
		print(n - len(b) + 2)
	else:
		print(n - len(b) + 1)
else:
	print(n)


n = int(input())
a = list(map(int, input().split()))
c = 0
f = 0
li = [0] * n
for i in range(n):
	li[a[i]] = i
for i in range(n):
	if a[i] == i:
		c += 1
	elif a[i] == li[i] and f == 0:
		f = 1
		c += 2
if c == n:
	print(c)
elif f == 0:
	print(c + 1)
else:
	print(c)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
a = rr()
arr = [[i, j] for (i, j) in enumerate(a)]
c = f = 0
hmap = defaultdict(lambda : False)
for (i, j) in arr:
	hmap[i, j] = True
for (i, j) in arr:
	if i == j:
		c += 1
	elif hmap[j, i] and (not f):
		c += 2
		f = 1
if c != n and (not f):
	c += 1
print(c)


n = int(input())
A = [int(x) for x in input().split()]
cnt_fixed_points = 0
for i in range(n):
	if A[i] == i:
		cnt_fixed_points += 1
flag = 0
for i in range(n):
	if A[i] != i and A[A[i]] == i:
		cnt_fixed_points += 2
		flag = 1
		break
if flag == 0 and cnt_fixed_points < n:
	print(cnt_fixed_points + 1)
else:
	print(cnt_fixed_points)


n = int(input())
a = list(map(int, input().split()))
e = sorted(a)
c = 0
x = 0
if e != a:
	x = 1
for i in range(n):
	if a[i] == i:
		c += 1
	elif a[a[i]] == i:
		x = 2
print(c + x)


numbers = int(input())
array = input().split(' ')
array = [int(x) for x in array]
points = 0
one = False
two = False
for i in range(numbers):
	if array[i] == i:
		points += 1
	else:
		j = array[i]
		if two == False:
			if array[i] == j and array[j] == i:
				two = True
		if one == False:
			if array[i] == j or array[j] == i:
				one = True
if two == True:
	points += 2
elif one == True:
	points += 1
print(points)


n = int(input())
a = list(map(int, input().split()))
t = False
rs = 0
for i in range(n):
	if a[i] != i:
		if a[a[i]] == i:
			t = True
	else:
		rs += 1
if rs == len(a):
	print(rs)
elif t:
	print(rs + 2)
else:
	print(rs + 1)


(cnt, swap) = (0, 0)
n = int(input())
L = [int(i) for i in input().split()]
for i in range(n):
	if i == L[i]:
		cnt += 1
	elif i == L[L[i]]:
		swap = 1
if cnt == n:
	print(cnt + swap)
else:
	print(cnt + swap + 1)


n = int(input())
a = list(map(int, input().split()))
ans = 0
t = 1
for i in range(n):
	if a[i] == i:
		ans += 1
	elif t and i == a[a[i]]:
		ans += 2
		t = 0
if t:
	print(min(ans + 1, n))
else:
	print(ans)


N = int(input())
A = list(map(int, input().split()))
(f1, f2) = (0, 0)
ct = 0
for (i, v) in enumerate(A):
	if A[v] == i and A[i] == i:
		ct += 1
	elif A[v] == i:
		f2 = 2
	else:
		f1 = 1
print(ct + max(f1, f2))


n = int(input())
mass = list(map(int, input().split()))
num = 0
flag = True
for i in range(n):
	if mass[i] == i:
		num += 1
	elif flag and mass[mass[i]] == i:
		num += 2
		flag = False
if flag and num != n:
	num += 1
print(num)


def solve(n, seq):
	times = 0
	temp_fixed = 0
	done = 0
	s = list(enumerate(seq))
	for (i, j) in s:
		if i == j:
			times += 1
		elif done == 0:
			if s[j][1] == i and j == s[j][0]:
				temp_fixed = 2
				done = 1
			elif s[j][1] == i or j == s[j][0]:
				temp_fixed = 1
	return times + temp_fixed
n = int(input())
seq = list(map(int, input().split()))
print(solve(n, seq))


from sys import stdin
n = int(input())
l = list(map(int, stdin.readline().split()))
i = 0
dict = {}
counter = 0
a = [int(i) for i in range(n)]
if a == l:
	print(n)
else:
	for e in l:
		if e == i:
			counter += 1
		else:
			dict[e] = i
		i += 1
	for (e, f) in dict.items():
		if dict.get(f) == e:
			print(counter + 2)
			break
	else:
		print(counter + 1)


input()
a = list(map(int, input().split()))
sum = 0
flag = False
for i in range(len(a)):
	if i == a[i]:
		sum += 1
	elif a[a[i]] == i and (not flag):
		sum += 2
		flag = True
if not flag and sum != len(a):
	sum += 1
print(sum)


n = int(input())
xs = [int(s) for s in input().split()]
fixed_points = 0
bonus = 0
for (i, x) in enumerate(xs):
	if x == i:
		fixed_points += 1
	elif xs[x] == i:
		bonus = 2
	else:
		bonus = max(bonus, 1)
print(fixed_points + bonus)


input()
a = [int(x) for x in input().split()]
count = 0
m = {}
yes = False
for (i, b) in enumerate(a):
	if b == i:
		count += 1
	elif not yes:
		if b in m and i == m[b]:
			yes = True
		else:
			m[i] = b
count += 2 if yes else 1 if len(m) else 0
print('%d' % count)


n = int(input())
count = 0
p = 0
l = list(map(int, input().split()))
for i in range(n):
	if l[i] == i:
		count += 1
	elif p < 2:
		if l[l[i]] == i:
			p = 2
		else:
			p = 1
print(count + p)


n = int(input())
a = [int(_) for _ in input().split()]
(count, s) = (0, 0)
for i in range(n):
	if a[i] == i:
		count += 1
	elif s < 2:
		if a[a[i]] == i:
			s = 2
		else:
			s = 1
print(count + s)


n = int(input(''))
lst = list(map(int, input().split()))
maximum = 0
first = True
second = True
for i in range(n):
	if lst[i] == i:
		maximum += 1
	elif first == True:
		if lst[i] < len(lst):
			if lst[lst[i]] == i:
				maximum += 2
				first = False
				if second == False:
					maximum -= 1
			elif lst[lst[i]] != lst[i] and second == True:
				maximum += 1
				second = False
print(maximum)


n = int(input())
sez = [int(i) for i in input().split()]
rez = 0
odg = 0
for i in range(n):
	if i == sez[i]:
		rez += 1
odg = rez
for j in range(n):
	if sez[j] == j:
		continue
	if sez[sez[j]] != sez[j]:
		odg = max(odg, rez + 1)
	if sez[sez[j]] == j:
		odg = max(odg, rez + 2)
print(odg)


def max_fixed(lst):
	max_increase = 0
	fixed = 0
	for (i, val) in enumerate(lst):
		if i == val:
			fixed += 1
			continue
		if max_increase < 2:
			if lst[val] == i:
				max_increase = max(max_increase, 2)
				continue
			if lst[val] != val:
				max_increase = max(max_increase, 1)
				continue
	return max_increase + fixed
_ = input()
print(max_fixed([int(x) for x in input().split()]))


n = int(input())
arr = [int(x) for x in input().split()]
(count, s) = (0, 0)
for i in range(n):
	if arr[i] == i:
		count += 1
	elif s < 2:
		if arr[arr[i]] == i:
			s = 2
		else:
			s = 1
print(count + s)


n = int(input())
s = input().split()
for i in range(n):
	s[i] = int(s[i])
fix = 0
flag = []
for i in range(n):
	if s[i] == i:
		fix += 1
		flag.append(1)
	else:
		flag.append(0)
swap = False
for i in range(n):
	if flag[i] == 1:
		continue
	else:
		t = s[i]
		if s[t] == i:
			fix += 2
			swap = True
			break
if swap == False and 0 in flag:
	fix += 1
print(fix)


n = int(input())
a = list(map(int, input().rstrip().split()))
t = 0
flag = 0
for i in range(n):
	if i == a[i]:
		t += 1
	elif a[i] < n and a[a[i]] == i:
		flag = 1
if t < n and flag == 1:
	t += 2
elif t < n:
	t += 1
print(t)


n = int(input())
lst = list(map(int, input().rstrip().split()))
(e, k) = (0, 0)
for i in range(n):
	if i == lst[i]:
		e += 1
	elif lst[lst[i]] == i:
		k = 1
if e == n:
	print(e)
elif k == 1:
	print(e + 2)
else:
	print(e + 1)


n = int(input())
s = list(map(int, input().split()))
(r1, r2) = (0, 1)
for i in range(n):
	if s[i] == i:
		r1 += 1
	elif i == s[s[i]]:
		r2 += 1
print(min(n, r1 + min(2, r2)))


def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	c = 0
	bestSwap = 0
	for (i, x) in enumerate(a):
		if i == x:
			c += 1
		elif a[x] != i:
			if bestSwap < 1:
				bestSwap = 1
		else:
			bestSwap = 2
	print(c + bestSwap)
main()


n = int(input())
arr = list(map(int, input().split()))
d = {}
ans = 0
flag = False
for (i, x) in enumerate(arr):
	d[i] = x
	if i == x:
		ans += 1
for i in d:
	if i != d[i] and d[d[i]] == i:
		ans += 2
		flag = True
		break
if flag:
	print(ans)
elif ans == len(arr):
	print(ans)
else:
	print(ans + 1)


n = int(input())
perm = [int(s) for s in input().split()]
vis = [0 for i in range(n)]
ans = 0
swap = 1
for i in range(n):
	if perm[i] == i and vis[i] == 0:
		ans += 1
for i in range(n):
	vis[i] = 1
	if perm[perm[i]] == i and vis[perm[i]] == 0:
		ans += 2
		swap = 0
		break
if swap == 1 and ans < n:
	ans += 1
print(ans)


import sys
import math
n = int(sys.stdin.readline())
an = [int(x) for x in sys.stdin.readline().split()]
res = 0
k1 = 0
k2 = 0
for i in range(n):
	if i == an[i]:
		res += 1
	elif an[an[i]] == i and an[i] != an[an[i]]:
		k2 = 2
	else:
		k1 = 1
print(res + max(k1, k2))


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
import heapq
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'
mod = 10 ** 9 + 7

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def ispali(s):
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	n = ii()
	arr = li()
	d = defaultdict(int)
	cnt = 0
	f = 0
	for i in range(n):
		if i == arr[i]:
			cnt += 1
		else:
			if d[arr[i] + 1] == i + 1:
				f = 1
			d[i + 1] = arr[i] + 1
	if f:
		cnt += 2
	elif cnt < n:
		cnt += 1
	print(cnt)
main()


n = int(input())
a = [*map(int, input().split())]
x = sum((a[i] == i for i in range(n)))
x += x < n
x += any((i != a[i] and i == a[a[i]] for i in range(n)))
print(x)


import sys
length = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
value_to_index = {}
index_to_value = {}
cnt = 0
temp = 0
final = 0
for (index, x) in enumerate(arr):
	value_to_index[x] = index
	index_to_value[index] = x
for (index, x) in enumerate(arr):
	if index == x:
		cnt += 1
	else:
		try:
			z = value_to_index[index]
			if z == x:
				temp = 2
			else:
				temp = 1
			final = max(final, temp)
		except KeyError:
			pass
print(cnt + final)


n = int(input())
A = list(map(int, input().split()))
C = []
T = False
i = 0
k = 0
while i < n:
	if A[i] != i:
		a = (max(A[i], i), min(A[i], i))
		C.append(a)
	else:
		k += 1
	i += 1
C.sort()
for i in range(len(C) - 1):
	if C[i] == C[i + 1]:
		T = True
		break
if n == k:
	print(k)
elif T:
	print(k + 2)
else:
	print(k + 1)


n = int(input())
l = [int(x) for x in input().split()]
d = {l[x]: x for x in range(n)}
ans = 0
for x in range(len(l)):
	if x == l[x]:
		ans += 1
ans1 = ans
for i in range(len(l)):
	if i != l[i]:
		j = l[i]
		if i == l[j] and j == l[i]:
			ans = max(ans, ans + 2)
			break
if ans1 != ans:
	print(ans)
elif ans1 == n:
	print(ans1)
else:
	print(ans1 + 1)