Buggy python submission (verdict: Runtime Error):
import sys
n = int(input())
a = [0] + list(map(int, input().split()))
if n == 2:
	print(-1)
	sys.exit()
used = [False for _ in range(n+1)]
hate = dict()
for x in a[1:]:
	if x in hate:
		hate[x] += 1
	else:
		hate[x] = 1
cur = 1
ng = 0
ans = []
while len(ans) < n:
	if len(hate) == 2:
		worst = False
		for k, v in hate.items():
			if v == n - len(ans) - 1 and not used[k]:
				worst = k
				break
		if worst:
			ans.append(k)
			used[k] = True
			ng = a[k]
			hate[a[k]] -= 1
			if hate[a[k]] == 0:
				del hate[a[k]]
			continue

	while used[cur]:
		cur += 1

	if cur == ng:
		cur_copy = cur+1
		while used[cur_copy]:
			cur_copy += 1
		ans.append(cur_copy)
		used[cur_copy] = True
		ng = a[cur_copy]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]
	else:
		ans.append(cur)
		used[cur] = True
		ng = a[cur]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]

print(*ans)

Fixed submission:
import sys
from itertools import permutations
n = int(input())
a = [0] + list(map(int, input().split()))
if n == 2:
	print(-1)
	sys.exit()
used = [False for _ in range(n+1)]
hate = dict()
for x in a[1:]:
	if x in hate:
		hate[x] += 1
	else:
		hate[x] = 1
cur = 1
ng = 0
ans = []
while len(ans) < n-3:
	if len(hate) == 2:
		worst = False
		for k, v in hate.items():
			if v == n - len(ans) - 1 and not used[k]:
				worst = k
				break
		if worst:
			ans.append(k)
			used[k] = True
			ng = a[k]
			hate[a[k]] -= 1
			if hate[a[k]] == 0:
				del hate[a[k]]
			continue

	while used[cur]:
		cur += 1

	if cur == ng:
		cur_copy = cur+1
		while used[cur_copy]:
			cur_copy += 1
		ans.append(cur_copy)
		used[cur_copy] = True
		ng = a[cur_copy]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]
	else:
		ans.append(cur)
		used[cur] = True
		ng = a[cur]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]

yet = [i for i in range(1, n+1) if not used[i]]
for p in permutations(yet):
	if p[0] != ng and p[1] != a[p[0]] and p[2] != a[p[1]]:
		ans += list(p)
		break

print(*ans)

Buggy python submission (verdict: Runtime Error):
import sys
n = int(input())
a = [0] + list(map(int, input().split()))
if n == 2:
	print(-1)
	sys.exit()
used = [False for _ in range(n+1)]
hate = dict()
for x in a[1:]:
	if x in hate:
		hate[x] += 1
	else:
		hate[x] = 1
cur = 1
ng = 0
ans = []
while len(ans) < n:
	if len(hate) == 2:
		worst = False
		for k, v in hate.items():
			if v == n - len(ans) - 1:
				worst = k
				break
		if worst:
			ans.append(k)
			used[k] = True
			ng = a[k]
			hate[a[k]] -= 1
			if hate[a[k]] == 0:
				del hate[a[k]]
			continue

	while used[cur]:
		cur += 1

	if cur == ng:
		cur_copy = cur+1
		while used[cur_copy]:
			cur_copy += 1
		ans.append(cur_copy)
		used[cur_copy] = True
		ng = a[cur_copy]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]
	else:
		ans.append(cur)
		used[cur] = True
		ng = a[cur]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]

print(*ans)

Fixed submission:
import sys
from itertools import permutations
n = int(input())
a = [0] + list(map(int, input().split()))
if n == 2:
	print(-1)
	sys.exit()
used = [False for _ in range(n+1)]
hate = dict()
for x in a[1:]:
	if x in hate:
		hate[x] += 1
	else:
		hate[x] = 1
cur = 1
ng = 0
ans = []
while len(ans) < n-3:
	if len(hate) == 2:
		worst = False
		for k, v in hate.items():
			if v == n - len(ans) - 1 and not used[k]:
				worst = k
				break
		if worst:
			ans.append(k)
			used[k] = True
			ng = a[k]
			hate[a[k]] -= 1
			if hate[a[k]] == 0:
				del hate[a[k]]
			continue

	while used[cur]:
		cur += 1

	if cur == ng:
		cur_copy = cur+1
		while used[cur_copy]:
			cur_copy += 1
		ans.append(cur_copy)
		used[cur_copy] = True
		ng = a[cur_copy]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]
	else:
		ans.append(cur)
		used[cur] = True
		ng = a[cur]
		hate[ng] -= 1
		if hate[ng] == 0:
			del hate[ng]

yet = [i for i in range(1, n+1) if not used[i]]
for p in permutations(yet):
	if p[0] != ng and p[1] != a[p[0]] and p[2] != a[p[1]]:
		ans += list(p)
		break

print(*ans)