There are $n$ friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.

For each friend the value $f_i$ is known: it is either $f_i = 0$ if the $i$-th friend doesn't know whom he wants to give the gift to or $1 \le f_i \le n$ if the $i$-th friend wants to give the gift to the friend $f_i$.

You want to fill in the unknown values ($f_i = 0$) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.

If there are several answers, you can print any.


-----Input-----

The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of friends.

The second line of the input contains $n$ integers $f_1, f_2, \dots, f_n$ ($0 \le f_i \le n$, $f_i \ne i$, all $f_i \ne 0$ are distinct), where $f_i$ is the either $f_i = 0$ if the $i$-th friend doesn't know whom he wants to give the gift to or $1 \le f_i \le n$ if the $i$-th friend wants to give the gift to the friend $f_i$. It is also guaranteed that there is at least two values $f_i = 0$.


-----Output-----

Print $n$ integers $nf_1, nf_2, \dots, nf_n$, where $nf_i$ should be equal to $f_i$ if $f_i \ne 0$ or the number of friend whom the $i$-th friend wants to give the gift to. All values $nf_i$ should be distinct, $nf_i$ cannot be equal to $i$. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself.

If there are several answers, you can print any.


-----Examples-----
Input
5
5 0 0 2 4

Output
5 3 1 2 4 

Input
7
7 0 0 1 4 0 6

Output
7 3 2 1 4 5 6 

Input
7
7 4 0 3 0 5 1

Output
7 4 2 3 6 5 1 

Input
5
2 1 0 0 0

Output
2 1 4 5 3

Difficulty: introductory

Starter code:

Solutions:
import sys
input = sys.stdin.readline

n=int(input())
F=list(map(int,input().split()))

USE=[0]*(n+1)

B=[]
for i in range(n):
    USE[F[i]]=1
    if F[i]==0:
        B.append(i+1)

A=[]
for i in range(1,n+1):
    if USE[i]==0:
        A.append(i)
        
for i in range(len(A)-1):
    if A[i]==B[i]:
        A[i],A[i+1]=A[i+1],A[i]

if A[-1]==B[-1]:
    A[-1],A[-2]=A[-2],A[-1]

ind=0

for i in range(n):
    if F[i]==0:
        F[i]=A[ind]
        ind+=1

print(*F)


n = int(input())
ls = list(map(int, input().split()))

nogive = set(list(range(1,n+1)))
noget = set(list(range(1,n+1)))
for i,e in enumerate(ls):
    if e != 0:
        nogive.remove(i+1)
        noget.remove(e)

prio = nogive.intersection(noget)

while len(nogive):
    if len(prio):
        giver = prio.pop()
        nogive.remove(giver)
    else:
        giver = nogive.pop()

    if len(prio):
        getter = prio.pop()
        noget.remove(getter)
    else:
        getter = noget.pop()
        if getter == giver:
            getter2 = noget.pop()
            noget.add(getter)
            getter = getter2
    ls[giver-1] = getter

for e in ls:
    print(e, end=' ')


    


N = int(input())

arr = list(map(int, input().split()))

uniq = set(arr)
uniq.remove(0)

not_found = [x for x in range(1,N+1) if x not in uniq]

import random
while True:
  random.shuffle(not_found)
  arr2 = arr.copy()
  idx = 0
  for i in range(N):
    if arr2[i] == 0:
      arr2[i] = not_found[idx]
      idx += 1
  if not any(i+1==x for i,x in enumerate(arr2)):
    print(' '.join(map(str, arr2)))
    break



n = int(input())
mas = list(map(int, input().split()))
cou = []
for i in range(n + 1):
	cou.append(0)
x = -1
for i in range(n):
	if mas[i] == 0:
		if x == -1:
			x = i
		else:
			y = i
for i in range(n):
	cou[mas[i]] = 1
num = n
for i in range (1, n + 1):
	if cou[i] != 1:
		cou[i] = 1
		while True:
			num -= 1
			if mas[num] == 0:
				mas[num] = i
				break
for i in range(n):
	if mas[i] == i + 1:
		if i == x:
			mas[i], mas[y] = mas[y], mas[i]
		else:
			mas[i], mas[x] = mas[x], mas[i]
print(*mas)


from sys import stdin,stdout
n=int(stdin.readline().strip())
s=list(map(int,stdin.readline().strip().split()))

vis=[False for i in range(n+10)]
for i in s:
    vis[i]=True
nu=[]
nu1=[]
for i in range(1,n+1):
    if not vis[i]:
        if s[i-1]==0:
            nu.append(i)
        else:
            nu1.append(i)
nu.sort()
while len(nu)>=2:
    if len(nu)%2!=0:
        s[nu[-3]-1]=nu[-1]
        s[nu[-1]-1]=nu[-2]
        s[nu[-2]-1]=nu[-3]
        nu.pop()
        nu.pop()
        nu.pop()
        continue
    s[nu[-1]-1]=nu[-2]
    s[nu[-2]-1]=nu[-1]
    nu.pop()
    nu.pop()


if len(nu)==1:
    s[nu[-1]-1]=nu1[-1]
    nu1.pop()
    nu1.append(nu[-1])
for i in range(n):
    if s[i]==0:
        s[i]=nu1[-1]
        nu1.pop()
print(*s)

    


n = int(input())
l = list(map(int,input().split()))
dostal = [0] * (n+1)
for i in range(n):
	dostal[l[i]] = 1
do_dania = []
ind = []
for i in range(n):
	if l[i] == 0:
		ind.append(i)
for i in range(1, n+1):
	if dostal[i] == 0:
		do_dania.append(i)
#print(do_dania)
j = 0
for i in range(n):
	if l[i] == 0:
		l[i] = do_dania[j]
		j += 1
for i in range(len(ind) - 1):
	if l[ind[i]] == ind[i] + 1:
		kk = l[ind[i]]
		l[ind[i]] = l[ind[i+1]]
		l[ind[i+1]] = kk
if l[ind[-1]] == ind[-1] + 1:
	kk = l[ind[-1]]
	l[ind[-1]] = l[ind[0]]
	l[ind[0]] = kk
print(*l)


N = int(input())

a = list(map(int,input().split()))

end = [False] * N
give = []

for i in range(N):

    if a[i] != 0:
        end[a[i] - 1] = True

    else:
        give.append(i)

want = []

for i in range(N):
    if not end[i]:
        want.append(i)

#print (give)
#print (want)

for i in range(len(give) - 1):

    if give[i] == want[i]:
        t = want[i+1]
        want[i+1] = want[i]
        want[i] = t

if give[-1] == want[-1]:
    t = want[-1]
    want[-1] = want[-2]
    want[-2] = t

for i in range(len(give)):

    a[give[i]] = want[i] + 1

print(" ".join(map(str,a)))


q=int(input())
w=list(map(int,input().split()))
e=[0]*q
r=[]
t=[]
for i in range(q):
    if w[i]==0:r.append(i)
    else:e[w[i]-1]=1
for i in range(q):
    if e[i]==0:t.append(i)
for i in range(len(r)):
    if r[i]==t[i]:
        if i==0:t[i],t[1]=t[1],t[i]
        else:t[i],t[0]=t[0],t[i]
for i in range(len(r)):
    w[r[i]]=t[i]+1
print(*w)

import random
n = int(input())
a = [int(x) for x in input().split()]
j = 1
b = [0] * (n+1)
for i in a:
  b[i] = 1
c = []
for i,j in enumerate(b):
  if j == 0:
    c.append(i)
c = set(c)
for i in range(1,n+1):
  i = i - 1
  if a[i] == 0:
    if i + 1 in c:
      for k in c:
        if k!=i+1:
          a[i] = k
          c.remove(k)
          break
for i in range(1,n+1):
  i = i - 1
  if a[i] == 0:
    for k in c:
      if k!=i+1:
        a[i] = k
        c.remove(k)
        break
for i in a:
  print(i,end=" ")

n=int(input())
a=list(map(int,input().split()))
vis=[False for i in range(n+1)]
c={}
for i in range(n):
    if a[i] is not 0:
        vis[a[i]]=True
    else:
        c[i]=True
d={}
for i in range(1,n+1):
    if vis[i]==False:
        d[i]=True
delete=[]
for i in c:
    if i+1 in d:
        k=0
        for j in d:
            if j is not i+1:
                a[i]=j
                k=j
                break
        d.pop(k)
        delete.append(i)
for i in delete:
    c.pop(i)
p=[i for i in c]
q=[i for i in d]
for i in range(len(p)):
    a[p[i]]=q[i]
print(" ".join(map(str,a)))

import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

def solve():
	n = mint()
	f = list(mints())
	w = [False]*(n+1)
	p = [False]*(n+1)
	for i in f:
		p[i] = True
	q = []
	for i in range(1, n+1):
		if w[i] or p[i]:
			continue
		s = i
		e = i
		w[i] = True
		ok = True
		while f[e-1] != 0:
			e = f[e-1]
			if e == s:
				ok = False
				break
			w[e] = True
		if ok:
			q.append((s,e))
	for i in range(len(q)-1):
		f[q[i][1]-1] = q[i+1][0]
	f[q[-1][1]-1] = q[0][0]
	#print(q)
	print(' '.join(map(str,f)))

#for i in range(mint()):
solve()


n=int(input())
l=list(map(int,input().split()))
new=[-1]*n
for i in l:
	if i!=0:
		new[i-1]=0
d=[]
for i in range(n):
	if new[i]==-1:
		d.append(i+1)
loc=0
ans=[]
index=[]
flag=0
for i in range(n):
	if l[i]!=0:
		ans.append(l[i])
	else:
		if d[loc]!=i+1 and flag==0:
			ans.append(d[loc])
			if len(index)==0:
				index.append(i)
		else:
			if len(index)!=0:
				ans.append(d[loc])
				ans[-1],ans[index[-1]]=ans[index[-1]],ans[-1]
				flag=0
			else:
				flag=1
				ans.append(d[loc])
				if len(index)==0:
					index.append(i)
		loc+=1
print (*ans)

n = int(input())
a = list(map(int, input().split()))
send = []
recv = []
vis = [0] * n
for i in range(n):
    if a[i] == 0:
        send.append(i+1)
    else:
        vis[a[i]-1] = 1
for i in range(n):
    if vis[i] == 0:
        recv.append(i+1)
send.sort()
recv.sort()
recv = list(set(send) & set(recv)) + list(set(recv) - set(send))
p = 0
for i in range(len(send)):
    while recv[p] == -1:
        p = (p+1) % len(recv)
    if send[i] == recv[p]:
            p = (p+1) % len(recv)
            while recv[p] == -1:
                p = (p+1) % len(recv)
    a[send[i]-1] = recv[p]
    recv[p] = -1
    p = (p+1) % len(recv)
print(*a)        


import sys

n, *f = map(int, sys.stdin.read().split())
f = [None] + f

def main():
    no_receieve = set(range(1, n+1)) - set(f[1:])
    if not no_receieve:
        return f[1:]
    
    no_send = []
    for i in range(1, n+1):
        if f[i] == 0:
            no_send.append(i)
    
    no_send.sort()
    no_receieve = sorted(no_receieve, reverse=True)

    for i in range(len(no_send)):
        if no_send[i] == no_receieve[i]:
            if i >= 1:
                no_send[i], no_send[i-1] = no_send[i-1], no_send[i]
            else:
                no_send[0], no_send[1] = no_send[1], no_send[0]
    
    for i in range(len(no_send)):
        f[no_send[i]] = no_receieve[i]
    
    return f[1:]

def __starting_point():
    ans = main()
    print(*ans, sep=' ')
__starting_point()

from sys import stdin
from collections import deque
mod = 10**9 + 7
import sys
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq

# n,k = map(int,input().split())
# x = int(input())
# s = str(x)
#
# ans = ['0']*n
#
# flag = 0
#
# for i in range(n):
#   if i-k>=0:
#       ans[i] = ans[i-k]
#       if ans[i]<s[i]:
#           flag = 1
#           break
#   else:
#       ans[i] = s[i]
# s1 = ''.join(ans)
# if not flag:
#     print(n)
#     print(s1)
#     return
# flag = 0
# ans = ['0']*n
#
# for i in range(k-1,-1,-1):
#     if s[i] == '9':
#         continue
#     else:
#         ans[i] = str(int(s[i]) + 1)
#         flag = 1
#
#         for j in range(i+1,k-1):
#             ans[j] = '0'
#         for j in range(i):
#             ans[j] = s[j]
#         break
# if flag:
#
#  for i in range(k,n):
#    if i-k<0:
#      continue
#
#    else:
#        ans[i] = ans[i-k]
#
#
#  print(len(ans))
#  print(''.join(ans))
#
# else:
#
#     if s[0] != '9':
#         for i in range(n):
#             if i-k<0:
#               if i == 0:
#                  ans[i] = str(int(s[i]) + 1)
#               else:
#                   ans[i] = '0'
#             else:
#                 ans[i] = ans[i-k]
#         print(len(ans))
#         print(''.join(ans))
#     else:
#         # print(ans)
#         ans+=['0']
#         for i in range(n+1):
#             if i-k<0:
#                 if i == 0:
#                     ans[i] = '1'
#                 else:
#                     ans[i] = '0'
#             else:
#                 ans[i] = ans[i-k]
#         print(len(ans))
#         print(''.join(ans))
#
#




#
# t = int(input())
#
# for _ in range(t):
#
#     n,s = map(int,input().split())
#     l = list(map(int,input().split()))
#     pa = []
#     ans = 0
#     if sum(l)<=s:
#         print(0)
#         continue
#     for i in range(n-1):
#
#         if l[i]>l[i+1]:
#             if pa == []:
#               pa.append(l[i])
#               ans = i+1
#             else:
#                if pa[0]<l[i] and s-pa[0]>=0:
#
#                    s-=pa[0]
#                    pa.pop()
#                    pa.append(l[i])
#                    ans = i+1
#                    if s == 0:
#                        break
#                else:
#                    if s>=l[i]:
#                        s-=l[i]
#                    else:
#                        break
#
#         else:
#             if s>=l[i]:
#                 s-=l[i]
#             else:
#                 break
#
#     print(ans)





#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# t = int(input())
#
# for _ in range(t):
#
#     n,m = map(int,input().split())
#     l1 = list(map(int,input().split()))
#     l2 = list(map(int,input().split()))
#     hash = defaultdict(int)
#
#     seti = set(l2)
#     for i in range(n):
#         if l1[i] in seti:
#             hash[l1[i]] = i
#     st = []
#     ans = 0
#     s = set(l1)
#     for i in range(m):
#           if l2[i] in s:
#              if st == []:
#                  z = hash[l2[i]]
#                  st.append(z)
#                  ans+=2*z + 1
#              else:
#                  z = hash[l2[i]]
#                  # print(i,z)
#                  if st[0]>z:
#                      ans+=1
#                  else:
#                      ans+=2*(z-1)+1
#                      st.pop()
#                      st.append(z-1)
#
#     print(ans)
#
#
#



n = int(input())
l = list(map(int,input().split()))
la = [i+1 for i in range(n)]
s = set(la)
yo = set()
for i in l:
  if i is not 0:
    s.remove(i)
    yo.add(i)
k = list(s)
k.sort()
for i in range(n):
    if l[i] == 0:
        z = k.pop()
        l[i] = z

for i in range(n):
    if l[i] == i+1:

        for j in range(n):
          if l[j] not in yo:
            if l[j] == j+1 and i!=j:
                l[i],l[j] = l[j],l[i]
                break
            else:
                if l[j]!=i+1 and l[i]!=j+1:
                    l[i],l[j] = l[j],l[i]
                    break




        break

print(*l)




n = int(input())
l = list(map(int, input().strip().split()))
recieved = [False for i in range(n+1)]
recieved[0] = True
for i in range(len(l)):
    recieved[l[i]] = True
recievedNotGiven = []
givenNotRecieved = []
both = []
for i in range(1,n+1):
    if l[i-1] == 0 and recieved[i] == False:
        both.append(i)
    elif l[i-1] == 0:
        recievedNotGiven.append(i)
    elif recieved[i] == False:
        givenNotRecieved.append(i)
if len(both) != 1:
    for i in range(len(recievedNotGiven)):
        l[recievedNotGiven[i]-1] = givenNotRecieved[i]
    for i in range(len(both)):
        if i < len(both)-1:
            l[both[i]-1] = both[i+1]
        else:
            l[both[i]-1] = both[0]
else:
    l[both[0]-1] = givenNotRecieved[0]
    l[recievedNotGiven[0]-1] = both[0]
    for i in range(1,len(givenNotRecieved)):
        l[recievedNotGiven[i]-1] = givenNotRecieved[i]
for i in range(len(l)):
    print(l[i], end = " ")

n = int(input())
f = list(map(int, input().split()))
give = set(i for i in range(n))
get = set(i for i in range(n))
for i in range(n):
    if f[i] > 0:
        give.remove(i)
        get.remove(f[i]-1)
givel = list(give)
for x in givel:
    if x in get:
        give.remove(x)
        for j in get:
            if j == x:
                continue
            f[x] = j+1
            break
        get.remove(j)
givel = list(give)
for x in give:
    for j in get:
        f[x] = j+1
        break
    get.remove(j)
print(' '.join(map(str, f)))

n = int(input())
a = list(map(int, input().split()))
a = [-1] + a

z = []
s = []
f = []
r = [0] * (n + 1)
g = [0] * (n + 1)
for i in range(1, n+1):
    if a[i] != 0:
        g[i] = 1
        r[a[i]] = 1
for i in range(1, n + 1):
    if g[i] == 1 and r[i] == 0:
        s.append(i)
    elif g[i] == 0 and r[i] == 1:
        f.append(i)
    elif g[i] ==0 and r[i] == 0:
        z.append(i)
zp = 0
i = 1
# print(f)
# print(s)
for i in range(len(f)):
    # if i < len(f):
    fin = f[i]
    st = s[i]
    if (i == len(f) - 1) and len(z) == 1:
        a[z[0]] = st
        a[fin] = z[0]
        z.pop()
    else:
        a[fin] = st

if len(z) > 0:
    st = z[0]
    for i in range(len(z)):
        ze = z[i]
        if i < len(z) - 1:
            a[ze] = z[i + 1]
        else:
            a[ze] = st
print(*a[1:])




n = int(input())
arr = list(map(int, input().split()))
frds = list(range(1, n + 1))
remain = set(frds) - set(arr)
slots = set()
for i in range(len(arr)):
    if arr[i] == 0:
        slots.add(i + 1)
same = slots & remain
for i in same:
    x = remain.pop()
    if x-1 != i-1:
        arr[i-1] = x
    else:
        y = remain.pop()
        arr[i-1] = y
        remain.add(x)
for i in range(len(arr)):
    if arr[i] == 0:
        arr[i] = remain.pop()
print(*arr, sep=" ")