use std::io::Write;

#[derive(Default)]
struct Scanner {
    buffer: Vec<String>
}

impl Scanner {
    fn next<T: std::str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buffer.pop() {
                return token.parse().ok().expect("Failed parse");
            }
            let mut input = String::new();
            std::io::stdin().read_line(&mut input).expect("Failed read");
            self.buffer = input.split_whitespace().rev().map(String::from).collect();
        }
    }
}

struct Task {
    nedges: i32,
    nvertices: i32,
    directed: std::collections::HashMap<i32, std::vec::Vec<i32>>,
    undirected: std::vec::Vec<(i32, i32)>,
    position: std::collections::HashMap<i32, i32>,
}

impl Task {
    fn new() -> Self {
        return Self {
            nedges: 0,
            nvertices: 0,
            directed: std::collections::HashMap::new(),
            undirected: vec![],
            position: std::collections::HashMap::new(),
        };
    }

    fn read_input(&mut self, scan: &mut Scanner) -> () {
        self.nvertices = scan.next::<i32>();
        self.nedges = scan.next::<i32>();

        for _ in 0..self.nedges {
            let t = scan.next::<i32>();
            let from = scan.next::<i32>();
            let to = scan.next::<i32>();

            if 1 == t {
                if self.directed.contains_key(&from) {
                    self.directed.get_mut(&from).unwrap().push(to);
                } else {
                    self.directed.insert(from, vec![to]);
                }
            } else {
                self.undirected.push((from, to));
            }
        }
    }

    fn write_output(&mut self, out: &mut std::io::BufWriter<std::io::Stdout>) -> () {
        if !self.topsort() {
            writeln!(out, "NO");
            return;
        }

        for (u, vec) in &self.directed {
            for v in vec {
                if self.position[u] >= self.position[v] {
                    writeln!(out, "NO");
                    return;
                }
            }
        }

        writeln!(out, "YES");

        for (u, vec) in &self.directed {
            for v in vec {
                writeln!(out, "{} {}", *u, *v);
            }
        }

        for (u, v) in &self.undirected {
            let upos = {
                if self.position.contains_key(u) {
                    self.position[u]
                } else {
                    0
                }
            };
            let vpos = {
                if self.position.contains_key(v) {
                    self.position[v]
                } else {
                    0
                }
            };
            if upos < vpos {
                writeln!(out, "{} {}", *u, *v);
            } else {
                writeln!(out, "{} {}", *v, *u);
            }
        }
    }

    fn topsort(&mut self) -> bool {
        let mut queue = std::collections::VecDeque::<i32>::new();
        let mut ideg = std::collections::HashMap::<i32, i32>::new();
        let mut position = 1;

        for (_, vec) in &self.directed {
            for v in vec {
                if ideg.contains_key(v) {
                    ideg.insert(*v, ideg[v] + 1);
                } else {
                    ideg.insert(*v, 1);
                }
            }
        }

        for u in 1..=self.nvertices {
            if !ideg.contains_key(&u) {
                queue.push_back(u);
            }
        }

        if queue.len() == 0 {
            return false;
        }

        while queue.len() > 0 {
            let u = queue.pop_front().unwrap();
            self.position.insert(u, position);
            position += 1;

            if self.directed.contains_key(&u) {
                for v in &self.directed[&u] {
                    ideg.insert(*v, ideg[v] - 1);
                    if ideg[v] == 0 {
                        queue.push_back(*v);
                    }
                }
            }
        }

        return true;
    }
}

fn main() {
    let mut scan = Scanner::default();
    let out = &mut std::io::BufWriter::new(std::io::stdout());

    let ntests = scan.next::<i32>();
    for _ in 0..ntests {
        let mut task = Task::new();
        task.read_input(&mut scan);
        task.write_output(out);
    }
}


# -*- coding: utf-8 -*-

import sys

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

def read_int():
    return int(sys.stdin.readline().strip())

def solve():
    _ = read_int()
    friends = read_int_list()
    with_present = set()
    unknown = set()
    for idx, f in enumerate(friends, 1):
        if f > 0:
            with_present.add(f)
        else:
            unknown.add(idx)

    without_present = set(xrange(1, len(friends) + 1)) - with_present
    # print('with present', with_present)
    # print('without present', without_present)
    # print('unknown', unknown)

    hm = {}
    common = unknown.intersection(without_present)

    for u in common:
        removed = False
        if u in without_present:
            without_present.remove(u)
            removed = True
        el = without_present.pop()
        hm[u] = el
        if removed:
            without_present.add(u)

    unknown = unknown - common

    for u in unknown:
        el = without_present.pop()
        hm[u] = el

    # print('u', u, 'without present', without_present, 'hm', hm)
    # print('hm', hm)

    for i in xrange(len(friends)):
        idx = i + 1
        v = friends[i]
        if v == 0:
            friends[i] = hm[idx]

    sys.stdout.write(' '.join(map(str, friends)) + '\n')

solve()


N = input()
A = map(int, raw_input().split())

from random import shuffle as s

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

x, y = set(V), set(F)
C = list(i for i in x if i in y)

#print C, V, F

if len(C) > 1:
    l = len(C)
    s(C)
    for i in xrange(l):
        A[C[i] - 1] = C[(i + 1) % l]
    for i in C:
        x.remove(i)
        y.remove(i)
    V, F = list(x), list(y)

#print A, V, F

s(F)

l = len(F)
while 1:.
    s(F)
    t = [i for i in xrange(l) if V[i] == F[i]]
    if not t: break

for i in xrange(l):
    A[V[i] - 1] = F[i]

for i in A: print i,


N = input()
A = map(int, raw_input().split())

from random import shuffle as s

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

x, y = set(V), set(F)
C = list(i for i in x if i in y)

#print C, V, F

if len(C) > 1:
    l = len(C)
    s(C)
    for i in xrange(l):
        A[C[i] - 1] = C[(i + 1) % l]
    for i in C:
        x.remove(i)
        y.remove(i)
    V, F = list(x), list(y)

#print A, V, F

s(F)

l = len(F)
for i in xrange(3):
    s(F)
    t = [i for i in xrange(l) if V[i] == F[i]]
    if not t: break

for i in xrange(l):
    A[V[i] - 1] = F[i]

for i in A: print i,


N = input()
A = map(int, raw_input().split())

from random import shuffle as s

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

x, y = set(V), set(F)
C = list(i for i in x if i in y)

#print C, V, F

if len(C) > 1:
    l = len(C)
    s(C)
    for i in xrange(l):
        A[C[i] - 1] = C[(i + 1) % l]
    for i in C:
        x.remove(i)
        y.remove(i)
    V, F = list(x), list(y)

#print A, V, F

s(F)

l = len(F)
for i in xrange(30):
    s(F)
    t = [i for i in xrange(l) if V[i] == F[i]]
    if not t: break

for i in xrange(l):
    A[V[i] - 1] = F[i]

for i in A: print i,


N = input()
A = map(int, raw_input().split())

from random import shuffle as s

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

x, y = set(V), set(F)
C = list(i for i in x if i in y)

#print C, V, F

if len(C) > 1:
    l = len(C)
    s(C)
    for i in xrange(l):
        A[C[i] - 1] = C[(i + 1) % l]
    for i in C:
        x.remove(i)
        y.remove(i)
    V, F = list(x), list(y)

#print A, V, F

l = len(F)
while 1:
    s(F)
    t = [i for i in xrange(l) if V[i] == F[i]]
    if not t: break

for i in xrange(l):
    A[V[i] - 1] = F[i]

for i in A: print i,


N = input()
A = map(int, raw_input().split())

from random import shuffle as s

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

x, y = set(V), set(F)
C = list(i for i in x if i in y)

if len(C) > 1:
    l = len(C)
    s(C)
    for i in xrange(l):
        A[C[i] - 1] = C[(i + 1) % l]
    for i in C:
        x.remove(i)
        y.remove(i)
    V, F = list(x), list(y)

#print A, V, F

s(F)

l = len(F)
for i in xrange(3):
    s(F)
    t = [i for i in xrange(l) if V[i] == F[i]]
    if not t: break

for i in xrange(l):
    A[V[i] - 1] = F[i]

for i in A: print i,


N = input()
A = map(int, raw_input().split())

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

from random import shuffle as s
while 1:
    s(F)
    t = [i for i in xrange(n) if F[i] == V[i]]
    if not t:
        break

for i in xrange(n):
    A[V[i] - 1] = F[i]
for i in A: print i,


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
st=0
p=[]
for i in range(n):
    if a[i]==0:
        p.append(i)
        a[i]=d[st]
        st+=1
flag=1
pos=-1
for i in range(n):
    if a[i]==i+1 and flag:
        pos=i
        flag=0
    elif a[i]==i+1:
        a[pos],a[i]=a[i],a[pos]
        flag=1
if pos!=-1 and a[pos]==pos+1 and flag==0:
    a[pos],a[p[0]]=a[p[0]],a[pos]

for i in a:
    print i,


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
st=0
for i in range(n):
    if a[i]==0:
        a[i]=d[st]
        st+=1
flag=1
for i in range(n):
    if a[i]==i+1 and flag:
        pos=i
        flag=0
    elif a[i]==i+1:
        a[pos],a[i]=a[i],a[pos]
        flag=1
for i in a:
    print i,


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
st=0
for i in range(n):
    if a[i]==0:
        a[i]=d[st]
        st+=1
flag=1
for i in range(n):
    if a[i]==i+1 and flag:
        pos=i
        flag=0
    elif a[i]==i+1:
        a[pos],a[i]=a[i],a[pos]
        pos1=i
        flag=1
a[pos],a[pos1]=a[pos1],a[pos]
for i in a:
    print i,


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
p=[]
for i in range(n):
    if a[i]==0:
        p.append(i+1)
i=0
j=0
r=[]
l1=len(d)
l2=len(p)
while i<l1 and j<l2:
    if d[i]==p[j]:
        r.append(d[i])
        i+=1
        j+=1
    elif d[i]<p[j]:
        i+=1
    else:
        j+=1
nr=[]
l3=len(r)
i=0
j=0
while i<l3 and j<l2:
    if r[i]==p[j]:
        i+=1
        j+=1
    elif r[i]<p[j]:
        i+=1
    else:
        nr.append(p[j])
        j+=1
while j<l2:
    nr.append(p[j])
    j+=1
dp={}
st1=0
st2=0
for i in range(n):
    if i==len(d):
        break
    if st1<len(r):
        e1=r[st1]
        e2=d[i]
        if e1==e2 and len(nr)!=st2:
            dp[nr[st2]]=e2
            st2+=1
        elif e1==e2:
            dp[r[st1]]=d[len(d)-1]
            d[i],d[len(d)-1]=d[len(d)-1],d[i]
            st1+=1
        else:
            dp[r[st1]]=e2
            st1+=1
    elif st2<len(nr):
        dp[nr[st2]]=d[i]
        st2+=1
    else:
        break
for i in dp.keys():
    a[i-1]=dp[i]
for i in a:
    print i,


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
st=0
p=[]
for i in range(n):
    if a[i]==0:
        p.append(i)
        a[i]=d[st]
        st+=1
flag=1
pos=-1
st=0
ll=len(p)-1
r=[i for i in p]
for i in range(n):
    if a[i]==i+1:
        if i==p[st]:
            p[st],p[ll]=p[ll],p[st]
            a[i],a[p[st]]=a[p[st]],a[i]
        else:
            a[i],a[p[st]]=a[p[st]],a[i]
            st+=1

for i in a:
    print i,


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
p=[]
for i in range(n):
    if a[i]==0:
        p.append(i+1)
i=0
j=0
r=[]
l1=len(d)
l2=len(p)
while i<l1 and j<l2:
    if d[i]==p[j]:
        r.append(d[i])
        i+=1
        j+=1
    elif d[i]<p[j]:
        i+=1
    else:
        j+=1
nr=[]
l3=len(r)
i=0
j=0
while i<l3 and j<l2:
    if r[i]==p[j]:
        i+=1
        j+=1
    elif r[i]<p[j]:
        i+=1
    else:
        nr.append(p[j])
        j+=1
while j<l2:
    nr.append(p[j])
    j+=1
dp={}
st1=0
st2=0
for i in range(n):
    if st1<len(r):
        e1=r[st1]
        e2=d[i]
        if e1==e2 and len(nr)!=st2:
            dp[nr[st2]]=e2
            st2+=1
        elif e1==e2:
            print d[n-1]
            dp[r[st1]]=d[n-1]
            d[i],d[n-1]=d[n-1],d[i]
        else:
            dp[r[st1]]=e2
            st1+=1
    elif st2<len(nr):
        dp[nr[st2]]=d[i]
        st2+=1
    else:
        break
for i in dp.keys():
    a[i-1]=dp[i]
print a


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
p=[]
for i in range(n):
    if a[i]==0:
        p.append(i+1)
i=0
j=0
r=[]
l1=len(d)
l2=len(p)
while i<l1 and j<l2:
    if d[i]==p[j]:
        r.append(d[i])
        i+=1
        j+=1
    elif d[i]<p[j]:
        i+=1
    else:
        j+=1
nr=[]
l3=len(r)
i=0
j=0
while i<l3 and j<l2:
    if r[i]==p[j]:
        i+=1
        j+=1
    elif r[i]<p[j]:
        i+=1
    else:
        nr.append(p[j])
        j+=1
while j<l2:
    nr.append(p[j])
    j+=1
dp={}
st1=0
st2=0
for i in range(n):
    if i==len(d):
        break
    if st1<len(r):
        e1=r[st1]
        e2=d[i]
        if e1==e2 and len(nr)!=st2:
            dp[nr[st2]]=e2
            st2+=1
        elif e1==e2:
            dp[r[st1]]=d[len(d)-1]
            d[i],d[len(d)-1]=d[len(d)-1],d[i]
            st1+=1
        else:
            dp[r[st1]]=e2
            st1+=1
    elif st2<len(nr):
        dp[nr[st2]]=d[i]
        st2+=1
    else:
        break
for i in dp.keys():
    a[i-1]=dp[i]
print a


n=input()
a=[int(i) for i in raw_input().split()]
b=[i for i in a]
b.sort()
d=[]
x=0
for i in b:
    if i==0:
        x+=1
for i in range(1,n+1):
    if x<n:
        if b[x]!=i:
            d.append(i)
        else:
            x+=1
    else:
        d.append(i)
st=0
for i in range(n):
    if a[i]==0:
        a[i]=d[st]
        st+=1
flag=1
pos1=-1
for i in range(n):
    if a[i]==i+1 and flag:
        pos=i
        flag=0
    elif a[i]==i+1:
        a[pos],a[i]=a[i],a[pos]
        pos1=i
        flag=1
if pos1!=-1 and a[pos1]==pos1+1:
    a[pos],a[pos1]=a[pos1],a[pos]
for i in a:
    print i,


import heapq
n = int(raw_input())
numbers = map(int, raw_input().split())
set_remaining = list(set(range(1, n + 1)) - set(numbers))
set_remaining = [-1*i for i in set_remaining]
heapq.heapify(set_remaining)

for i in range(n):
    if numbers[i] == 0:
        temp = -1 * heapq.heappop(set_remaining)
        if temp == i + 1:
            temp2 = -1 * heapq.heappop(set_remaining)
            heapq.heappush(set_remaining, -1 * temp)
            temp = temp2
        numbers[i] = temp
print ' '.join(map(str, numbers))


n = int(raw_input())
numbers = map(int, raw_input().split())
set_remaining = set(range(1, n + 1)) - set(numbers)
for i in range(n):
    if numbers[i] == 0:
        numbers[i] = max(set(set_remaining) - set([i + 1]))
        set_remaining.remove(numbers[i])
print ' '.join(map(str, numbers))


import heapq
n = int(raw_input())
numbers = map(int, raw_input().split())
set_remaining = list(set(range(1, n + 1)) - set(numbers))
set_remaining = [-1*i for i in set_remaining]
heapq.heapify(set_remaining)
last_index = None
for i in range(n):
    if numbers[i] == 0:
        temp = -1 * heapq.heappop(set_remaining)
        if temp == i + 1:
            if not set_remaining:
                break
            temp2 = -1 * heapq.heappop(set_remaining)
            heapq.heappush(set_remaining, -1 * temp)
            temp = temp2
        last_index = i
        numbers[i] = temp

for i in range(n):
    if numbers[i] == 0:
        numbers[i]= numbers[last_index]
        numbers[last_index] = i +1
        break
print ' '.join(map(str, numbers))


n = int(raw_input())
numbers = map(int, raw_input().split())
set_remaining = set(range(1, n + 1)) - set(numbers)
set_indeces = set([i + 1 for i in range(n) if numbers[i] == 0])

for i in range(n):
    if numbers[i] == 0:
        set_intersection = set_indeces.intersection(set_remaining - set([i + 1]))
        if set_intersection:
            numbers[i] = list(set_indeces.intersection(set_remaining - set([i + 1])))[0]
            set_indeces.remove(i + 1)
        else:
            numbers[i] = list(set_remaining - set([i + 1]))[0]
        set_remaining.remove(numbers[i])
print ' '.join(map(str, numbers))


N = input()
A = map(int, raw_input().split())

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

from random import shuffle as s
while 1:
    s(F)
    t = [i for i in xrange(n) if F[i] == V[i]]
    if not t:
        break
    #
for i in xrange(n):
    A[V[i] - 1] = F[i]
for i in A: print i,


n=input()
a=map(int,raw_input().split())
b=set(a)
r=set(range(n+1))-b
s=[]
for i in range(n):
	if a[i]==0:
		s.append(i+1)
s=set(s)
x=r&s
r=r-x
s=s-x
x=list(x)
r=list(r)
s=list(s)
#print r,s,x
for i in range(len(x)):
	a[x[i]-1]=x[(i+1)%len(x)]
for i in range(len(s)):
	a[s[i]-1]=r[i]
if len(x)==1:
	a[x[0]-1],a[s[0]-1]=a[s[0]-1],a[x[0]-1]
for i in a:
	print i,


if __name__ == "__main__":
    n = int(raw_input())
    f_list = [int(x) for x in raw_input().split()]
    idx_list = []
    val_s = set(xrange(1, n + 1))
    for i, f in enumerate(f_list):
        if f == 0:
            idx_list.append(i + 1)
        else:
            val_s.remove(f)
    val_list = list(val_s)
    idx_s = set(idx_list)
    res = []
    hold = None
    for i, v in zip(idx_list, val_list):
        if i == v:
            if hold:
                res.append(v)
                res.append(hold)
                hold = None
            else:
                hold = v
        else:
            res.append(v)
            if hold:
                res.append(hold)
                hold = None
    if hold:
        res.append(res[0])
        res[0] = hold
    j = 0
    for i, v in enumerate(f_list):
        if v == 0:
            f_list[i] = res[j]
            j = j + 1
    for x in f_list:
        print x,


'''input
7
7 4 0 3 0 5 1
'''
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
import bisect
n = input()
a = I()
arr = []
vis = [0]*n
for i in a:
	vis[i-1] = 1
# print vis
for i in range(n):
	if not vis[i]:
		arr.append(i+1)
# arr.reverse()
# print arr
for j,i in enumerate(a):
	if i==0:
		if arr[-1] == j+1:
			(arr[-1],arr[-2]) = (arr[-2],arr[-1])
		print arr.pop(),
	else:
		print i,


'''input
5
2 1 0 0 0
'''
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
import bisect
n = input()
a = I()
arr = []
vis = [0]*n
for i in a:
	if i!=0:
		vis[i-1] = 1
for i in range(n):
	if not vis[i]:
		arr.append(i+1)
# arr.reverse()
# print arr
for j,i in enumerate(a):
	if i==0:
		if arr[-1] == j+1:
			(arr[-1],arr[0]) = (arr[0],arr[-1])
		print arr.pop(),
	else:
		print i,


'''input
3
0 0 1
'''
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
import bisect
n = input()
a = I()
arr = []
vis = [0]*n
ac = []
for j,i in enumerate(a):
	if i!=0:
		vis[i-1] = 1
	else:
		ac.append(j+1)
for i in range(n):
	if not vis[i]:
		arr.append(i+1)
# arr.reverse()
# print arr,ac
flag = 0
d = {}
for j,i in enumerate(a):
	if i==0:
		if arr[-1] == j+1:
			if len(arr) == 1:
				flag = j+1
			(arr[-1],arr[0]) = (arr[0],arr[-1])
		d[j+1] = arr.pop()
	else:
		d[j+1] = i
if flag:
	for i in ac:
		if flag != i:
			(d[i],d[flag]) = (d[flag],d[i])
			break
# print d
for i in d:
	print d[i],


'''input
7
7 4 0 3 0 5 1
'''
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
import bisect
n = input()
a = I()
arr = []
vis = [0]*n
for i in a:
	vis[i-1] = 1
# print vis
for i in range(n):
	if not vis[i]:
		arr.append(i+1)
# arr.reverse()
# print arr
for j,i in enumerate(a):
	if i==0:
		if arr[-1] == j+1:
			(arr[-1],arr[0]) = (arr[0],arr[-1])
		print arr.pop(),
	else:
		print i,


'''input
5
2 1 0 0 0
'''
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
import bisect
n = input()
a = I()
arr = []
vis = [0]*n
for i in a:
	if i!=0:
		vis[i-1] = 1
for i in range(n):
	if not vis[i]:
		arr.append(i+1)
# arr.reverse()
# print arr
for j,i in enumerate(a):
	if i==0:
		if arr[-1] == j+1:
			(arr[-1],arr[-2]) = (arr[-2],arr[-1])
		print arr.pop(),
	else:
		print i,


'''input
3
0 0 1
'''
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
import bisect
n = input()
a = I()
arr = []
vis = [0]*n
for i in a:
	if i!=0:
		vis[i-1] = 1
for i in range(n):
	if not vis[i]:
		arr.append(i+1)
# arr.reverse()
# print arr
ac = arr[::]
flag = 0
d = {}
for j,i in enumerate(a):
	if i==0:
		if arr[-1] == j+1:
			if len(arr) == 1:
				flag = j+1
			(arr[-1],arr[0]) = (arr[0],arr[-1])
		d[j+1] = arr.pop()
	else:
		d[j+1] = i
if flag:
	for i in ac:
		if flag != i:
			(d[i],d[flag]) = (d[flag],d[i])
			break
# print d
for i in d:
	print d[i],


N = input()
A = map(int, raw_input().split())

V = [i + 1 for i in xrange(N) if A[i] == 0]
f = [1] * (N + 1)
for i in A: f[i] = 0
F = [i for i in range(1, N + 1) if f[i]]

n = len(F)

from random import shuffle as s
while 1:
    s(F)
    t = [i for i in xrange(n) if F[i] == V[i]]
    if not t:
        break

for i in xrange(n):
    A[V[i] - 1] = F[i]
for i in A: print i,


#include <stdio.h>
int main()
{
    int i,n;
    scanf("%d",&n);
    int giftGiving[n];
    int giftGetting[n];
    int giftNot[n];
    int top=0,bottom=0;
    for (i=0; i<n; i++)
    {
        giftGetting[i]=0;
    }
    for (i=0; i<n; i++)
    {
        scanf("%d", &giftGiving[i]);
        giftGetting[giftGiving[i]-1]=1;
    }
    for (i=0; i<n; i++)
    {
        if (giftGetting[i]!=1)
        {
            giftNot[bottom++]=i+1;
        }
    }
    for (i=0; i<n; i++)
    {
        if (giftGiving[i]==0 && giftGetting[i]==0)
        {
            if (giftNot[top]==i+1)
            {
                giftGiving[i]=giftNot[--bottom];
            }
            else
            {
                giftGiving[i]=giftNot[top++];
            }
        }
    }
    for (i=0; i<n; i++)
    {
        if (giftGiving[i]==0)
        {
            giftGiving[i]=giftNot[top++];
        }
    }
    for (i=0; i<n; i++)
    {
        printf("%d ", giftGiving[i]);
    }
    /*printf("\n");
    for (i=0; i<n; i++)
    {
        printf("%d ", giftGetting[i]);
    }
    printf("\n");
    for (i=top; i<bottom; i++)
    {
        printf("%d ", giftNot[i]);
    }
    printf("\n");*/

    return 0;
}


#include <stdio.h>
int main()
{
    int i,n;
    scanf("%d",&n);
    int giftGiving[n];
    int giftGetting[n];
    int giftNot[n];
    int top=0,bottom=0;
    for (i=0; i<n; i++)
    {
        scanf("%d", &giftGiving[i]);
        giftGetting[giftGiving[i]-1]=1;
    }
    for (i=0; i<n; i++)
    {
        if (giftGetting[i]!=1)
        {
            giftNot[bottom++]=i+1;
        }
    }
    for (i=0; i<n; i++)
    {
        if (giftGiving[i]==0 && giftGetting[i]!=1)
        {
            if (giftNot[top]==i+1)
            {
                giftGiving[i]=giftNot[--bottom];
            }
            else
            {
                giftGiving[i]=giftNot[top++];
            }
        }
    }
    for (i=0; i<n; i++)
    {
        if (giftGiving[i]==0)
        {
            giftGiving[i]=giftNot[top++];
        }
    }
    for (i=0; i<n; i++)
    {
        printf("%d ", giftGiving[i]);
    }
    /*printf("\n");
    for (i=0; i<n; i++)
    {
        printf("%d ", giftGetting[i]);
    }
    printf("\n");
    for (i=top; i<bottom; i++)
    {
        printf("%d ", giftNot[i]);
    }
    printf("\n");*/

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
void A() {
	int t, h, m;
	int result = 0;
	scanf("%d", &t);
	while (t--) {
		result = 0;
		scanf("%d %d", &h, &m);
		m = 60 - m;
		h = 23 - h;
		result = h * 60 + m;
		printf("%d\n", result);
	}

}

void B() {
	int t;
	int n, k;
	int max;
	int result;
	//���� ���� �޴� ���̿� ���� �޴� ������
	//ĵ�� �� ���̴� 1����
	//�ִ� k/2 ���� ���̵��� �ִ��� ������ ���� �� �־�
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &k);
		max = k / 2;
		int i = n / k;

		n %= k; //���� ����
		result = i * k;
		result += n > max ? max : n;
		printf("%d\n", result);
	}
}

void C() {
	int n;
	char* check = calloc(200001, 1);
	int i = 0;
	int j;
	int num;
	int remember;
	int before;
	int* arr = calloc(200001, sizeof(int));
	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		scanf("%d", &num);
		check[num] = 1;
		arr[i] = num;
	}

	remember = -1;
	before = 0;
	for (j = 0, i = 1; i <=n; i++) {

		if (arr[i] == 0) {
			if (remember == -1) {
				while (check[++j] != 0);

				if (i == j) {
					remember = j++;
					while (check[j] != 0)
					{
						j++;
					}
					arr[i] = j++;

					if (j > n) {
						arr[i] = arr[before];
						arr[before] = i;
					}
				}
				else {
					arr[i] = j++;
				}

			}

			else {
				arr[i] = remember;
				remember = -1;
				j--;
			}

			before = i;
		}
	}
	//������ �Ἥ �Ҵ� �� �ֵ� �� �ٲ��� �� �ִ� �� ���ڼ� �־�����
	//������ ������� �� �տ��� ó���� ��ó�� ���ָ� �ɵ�
	//������ �ڽſ��� ������ �ָ� �ȵ�
	for (i = 1; i <= n; i++) {
		printf("%d ", arr[i]);
	}
}
int main() {
	C();
}


#include<stdio.h>
#include<stdlib.h>

void A() {
	int t, h, m;
	int result = 0;
	scanf("%d", &t);
	while (t--) {
		result = 0;
		scanf("%d %d", &h, &m);
		m = 60 - m;
		h = 23 - h;
		result = h * 60 + m;
		printf("%d\n", result);
	}

}

void B() {
	int t;
	int n, k;
	int max;
	int result;
	//���� ���� �޴� ���̿� ���� �޴� ������
	//ĵ�� �� ���̴� 1����
	//�ִ� k/2 ���� ���̵��� �ִ��� ������ ���� �� �־�
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &k);
		max = k / 2;
		int i = n / k; //a�� ���ϴ� ����

		n %= k; //���̵鿡�� a���� ���� ���� �� ���� ������ ����
		result = i * k;

		result += n > max ? max : n;
		// ���� ������ ����ŭ k/2�� ���� �ʴ� �ѵ� ������
		//���̵鿡�� ���� �й�
		printf("%d\n", result);
	}
}

int findNum(char* check, int start, int end) {
	while (check[start] != 0)
		start++;
	if (start > end)
		return -1;
	return start;
}
void C() {
	int* f = calloc(200001, sizeof(int));
	char* check = calloc(200001, sizeof(char));
	int n;
	int input;
	int j = 1;
	int index = 1;
	int remember = -1;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++) {
		scanf("%d", &input);
		f[i] = input;
		check[input] = 1;
	}

	for (int i = 1; i <= n; i++) {
		if (f[i] == 0) {
			if (remember == -1) {
				j = findNum(check, j, n); //ã��
				if (i == j) { //�� ��
					remember = j++; //�ϴ� ���
					j = findNum(check, j, n); //������ ���� �� �ִ°� ã
					if (j == -1) { //������ ����...
						f[i] = f[index];
						f[index] = remember;
					}
					else {
						f[i] = j++;
					}
				}

				else {
					f[i] = j++;
				}
			}
			else {
				f[i] = remember;
				remember = -1;
			}

			index = i;
		}
	}

	for (int i = 1; i <= n; i++) {
		printf("%d ", f[i]);
	}
}

int main() {
	C();
}


#include<stdio.h>
#include<stdlib.h>
void A() {
	int t, h, m;
	int result = 0;
	scanf("%d", &t);
	while (t--) {
		result = 0;
		scanf("%d %d", &h, &m);
		m = 60 - m;
		h = 23 - h;
		result = h * 60 + m;
		printf("%d\n", result);
	}

}

void B() {
	int t;
	int n, k;
	int max;
	int result;
	//���� ���� �޴� ���̿� ���� �޴� ������
	//ĵ�� �� ���̴� 1����
	//�ִ� k/2 ���� ���̵��� �ִ��� ������ ���� �� �־�
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &k);
		max = k / 2;
		int i = n / k;

		n %= k; //���� ����
		result = i * k;
		result += n > max ? max : n;
		printf("%d\n", result);
	}
}

void C() {
	int n;
	char* check = calloc(200001, 1);
	int i = 0;
	int j;
	int num;
	int remember;
	int before;
	int* arr = calloc(200001, sizeof(int));
	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		scanf("%d", &num);
		check[num] = 1;
		arr[i] = num;
	}

	remember = -1;
	before = 0;
	for (j = 0, i = 1; i <= n; i++) {

		if (arr[i] == 0) {
			if (remember == -1) {
				while (check[++j] != 0);

				if (i == j) {
					remember = j++;
					while (check[j] != 0)
					{
						j++;
					}
					arr[i] = j++;

					if (i==n && j > n) {
						arr[i] = arr[before];
						arr[before] = i;
					}
				}
				else {
					arr[i] = j++;
				}

			}

			else {
				arr[i] = remember;
				remember = -1;
				j--;
			}

			before = i;
		}
	}
	for (i = 1; i <= n; i++) {
		printf("%d ", arr[i]);
	}

}
int main() {
	C();
}


#include<stdio.h>
#include<stdlib.h>
void A() {
	int t, h, m;
	int result = 0;
	scanf("%d", &t);
	while (t--) {
		result = 0;
		scanf("%d %d", &h, &m);
		m = 60 - m;
		h = 23 - h;
		result = h * 60 + m;
		printf("%d\n", result);
	}

}

void B() {
	int t;
	int n, k;
	int max;
	int result;
	//���� ���� �޴� ���̿� ���� �޴� ������
	//ĵ�� �� ���̴� 1����
	//�ִ� k/2 ���� ���̵��� �ִ��� ������ ���� �� �־�
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &k);
		max = k / 2;
		int i = n / k;

		n %= k; //���� ����
		result = i * k;
		result += n > max ? max : n;
		printf("%d\n", result);
	}
}

void C() {
	int n;
	char* check = calloc(200001, 1);
	int i = 0;
	int j;
	int num;
	int remember;
	int before;
	int* arr = calloc(200001, sizeof(int));
	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		scanf("%d", &num);
		check[num] = 1;
		arr[i] = num;
	}

	remember = -1;
	before = 0;
	for (j = 0, i = 1; i <= n; i++) {

		if (arr[i] == 0) {
			if (remember == -1) {
				while (check[++j] != 0);

				if (i == j) {
					remember = j++;
					while (check[j] != 0)
					{
						j++;
					}
					arr[i] = j++;

					if (j > n) {
						if (before != 0) {
							arr[i] = arr[before];
							arr[before] = i;
						}
					}
				}
				else {
					arr[i] = j++;
				}

			}

			else {
				arr[i] = remember;
				remember = -1;
				j--;
			}

			before = i;
		}
	}

	for (i = 1; i <= n; i++) {
		printf("%d ", arr[i]);
	}

}
int main() {
	C();
}


//
//  main.c
//  Contest
//
//  Created by Mihai Esanu on 12/27/19.
//  Copyright © 2019 Mihai Esanu. All rights reserved.

#include <stdio.h>

int main() {
    long long t, n[50000];
    scanf("%lld", &t);
    for(long long i = 1; i <= t; i++)
    {
        scanf("%lld", &n[i]);
    }

    for(long long i = 1; i <= t; i++)
    {
        if(n[i] == 0)
        {
            int l = 0, k = 1;
            long long value;
            while(l == 0)
            {
            for(long long j = 1; j < t; j++)
            {
                if(n[j] == k)
                {
                    value = j;
                    l = 0;
                    break;
                }

            }
                k++;
            }
            n[i] = value;
        }
    }
        return 0;
}


#pragma region kyopuro_templates
#pragma GCC optimize("Ofast")
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<stdbool.h>
#include<assert.h>
#include<time.h>
#include<ctype.h>
typedef long long ll;
typedef long double ld;
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define rrep(i,l,r)for(ll i=(l);i>=(r);i--)
#define INF (1LL<<62)
#define MOD1 1000000007
#define MOD2 998244353
#define MAX_N (1 << 20)
#define YES printf("Yes\n")
#define NO printf("No\n")
#define PN printf("\n")
#define charsize 100005 //10^5+5
#define PI 3.141592653589793238

void swap(ll *a, ll *b){ll c;c=*b;*b=*a;*a= c;}
void cin(ll *n){ scanf("%lld",&(*n)); }
ll max2(ll a,ll b){return a>=b?a:b;}
ll min2(ll a,ll b){return a>=b?b:a;}
ll min3(ll a, ll b, ll c){return (a<=b && a<=c) ? a : b<=c ? b : c;}
ll max3(ll a, ll b, ll c){return (a>=b && a>=c) ? a : b>=c ? b : c;}
ll minn(ll n, ll a[]){ll b=INF;rep(i,0,n) b=min2(b,a[i]);return b;}
ll maxn(ll n, ll a[]){ll b=-INF;rep(i,0,n) b=max2(b,a[i]);return b;}
ll POW(ll a, ll b){ll c=1;rep(i,0,b) c*=a;return c;}
double POW_d(double a, double b){double c=1;rep(i,0,b) c*=a;return c;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll mod_MOD1(ll n){n+= n<0?((-n)/MOD1+1)*MOD1:0; return n%=MOD1;}
ll mod_p(ll n ,ll p){n+= n<0?((-n)/p+1)*p:0; return n%=p;}
ll change_into_num(char s[] , ll len, ll p){ return !p ? 0 : POW(10,p-1)*(s[len-p]-'0') + change_into_num(s,len,p-1); }
ll digits(ll a, ll b){return a/b?1+digits(a/b,b):1;}
ll base(ll n, ll a, ll i){return i==1?n%a:base(n/a,a,i-1);}
ll is_inArr1(ll x, ll n){ return ( x<0 || x>=n ) ? 0 : 1 ; }
ll is_inArr2(ll y, ll x, ll h, ll w){ return ( y<0 || y>=h || x<0 || x>=w ) ? 0 : 1 ; }

void lr_lower( int *l, int *r, ll am, ll val , int type ){ (type<3) ? ( am < val ?  ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ) : ( am <= val ? ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ); }
void lr_upper( int *l, int *r, ll am, ll val , int type ){ (type<3) ? ( am <= val ?  ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ) : ( am < val ? ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ); }
int cmp_lower( ll a, ll b, int type ){ return (type==1) ? ( a==b ? 1 : 0 ) : (type==2) ? ( a>=b ? 1 : 0 ) : ( a>b ? 1 : 0 ) ; }
int cmp_upper( ll a, ll b, int type ){ return (type==1) ? ( a==b ? 1 : 0 ) : (type==2) ? ( a<=b ? 1 : 0 ) : ( a<b ? 1 : 0 ) ; }
// return smallest p  which meets  a[p]==val :1  >=:2   >:3
ll lower_bound( ll a[], int l, int r, ll val , int type ){  while(r-l>1) lr_lower(&l,&r,a[ (l+r)/2 ],val,type);  return cmp_lower(a[l],val,type) ? l : cmp_lower(a[r],val,type) ? r : -1;  }
// return biggest p  which meets   a[p]==val :1  <=:2   <:3
ll upper_bound( ll a[], int l, int r, ll val , int type ){  while(r-l>1) lr_upper(&l,&r,a[ (l+r)/2 ],val,type); return cmp_upper(a[r],val,type) ? r : cmp_upper(a[l],val,type) ? l : -1; }
// count i  which meets ai==x
ll count(ll a[], int l, int r, ll x){  int p = lower_bound(a,l,r,x,1);  return p==-1 ? 0 : upper_bound(a,p,r,x,1)-p+1; }

ll *factors[2] , fac_cnt=0 , is_factor_prepared=0;
ll factor_pre(){  rep(i,0,1){ if(is_factor_prepared++) return 0; }  ll tmp=(1e6)/2+1, fac_tmp[tmp];  rep(i,0,tmp){fac_tmp[i]=i?2*i+1:2;}  rep(i,1,tmp){if(fac_tmp[i]){  repp(j,3,tmp/(2*i+1)+1,2 ){  if( j*(2*i+1)<tmp ) fac_tmp[ (j*(2*i+1)-1)/2 ]=0;  }  }else continue;}  rep(i,0,tmp){if(fac_tmp[i]){  rep(j,0,2){ factors[j] = realloc( factors[j] , sizeof(ll)*( fac_cnt +1 ) );  factors[j][j?fac_cnt++:fac_cnt]=j?0:fac_tmp[i];  }  } }  return 0;  }
ll factor(ll n, ll new_common_plus){ factor_pre();  rep(i,0,fac_cnt){ ll cnt=0; rep(j,0,1){ while( ( cnt+= n %factors[0][i]==0 ? 1 : 0 )  && (n/=factors[0][i]) %factors[0][i]==0 ) continue; } factors[1][i]= new_common_plus==1 ? cnt :  new_common_plus==2 ? max2(factors[1][i],cnt) : factors[1][i]+cnt ;  if( factors[0][i]> n ) break; } return n; }
ll judge_prime(ll n){ factor_pre(); rep(i,0,fac_cnt){ if(n<factors[0][i]*factors[0][i] || n==factors[0][i]) break; else if(n%factors[0][i]==0) n/=n; }  return n==1?0:1; }

ll *mf_arr,*inv_arr,*finv_arr,is_minv_made=0,is_mf_made=0,num_of_inv=2*1e6+10;
ll makeinv(ll n , ll mod){  rep(i,0,1){if(is_minv_made++) return 0;}  inv_arr = realloc(inv_arr, sizeof(ll)*2 );  finv_arr = realloc(finv_arr, sizeof(ll)*2 );  inv_arr[1]=1;finv_arr[0]=finv_arr[1]=1;  rep(i,2,n+1){  inv_arr = realloc(inv_arr, sizeof(ll)*(i+1) );  finv_arr = realloc(finv_arr, sizeof(ll)*(i+1) );  inv_arr[i]= mod - inv_arr[mod%i] * (mod / i) % mod;  finv_arr[i] = finv_arr[i - 1] * inv_arr[i] % mod;  }  return 0; }
ll make_mf(ll n, ll mod){  rep(i,0,1){ if(is_mf_made++) return 0; }  mf_arr = realloc(mf_arr, sizeof(ll)*2 );  ll x=1;  mf_arr[0]=mf_arr[1]=x;  rep(i,2,n+1){   x=x*i%mod;  mf_arr = realloc(mf_arr, sizeof(ll)*(i+1) );  mf_arr[i]=x; }  return 0;  }
ll m_inv(ll x, ll mod, ll is_fac ){ makeinv(num_of_inv,mod);  return is_fac?finv_arr[x]:inv_arr[x]; }
ll m_f(ll x, ll mod){  make_mf(num_of_inv,mod);  return mf_arr[x];  }
ll mod_nck(ll n, ll k, ll mod){ return m_f(n,mod)*m_inv(k,mod,1)%mod*m_inv(n-k,mod,1)%mod; }
ll m_p(ll r,ll n,ll mod){  ll t=1,s=r;  while(n>0){ t = (n&1) ? t*s%mod : t;  s=s*s%mod; n>>=1; }  return r?t:0;  }
ll m_mul2(ll a, ll b, ll mod){ return a*b%mod; }
ll m_mul3(ll a, ll b, ll c, ll mod){ return m_mul2(a*b%mod,c,mod); }
ll m_mul4(ll a, ll b, ll c, ll d, ll mod){ return m_mul3(a*b%mod,c,d,mod); }
ll m_mul5(ll a, ll b, ll c, ll d, ll e, ll mod){ return m_mul4(a*b%mod,c,d,e,mod); }

int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
int cmp_string( const void * a , const void * b ) {  return strcmp( (char *)a , (char *)b ); }  // qsort((void*)s,n,sizeof(s[0]),int_sort );
int cmp_char(const void * a, const void * b) { return *(char *)a - *(char *)b;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
void sort_string(int n,int size,char s[][size]){ qsort( (void*)s , n , sizeof(s[0]) , cmp_string ); }
void sort_char(char *s){ qsort( (void *)s , strlen(s) , sizeof(char) , cmp_char ); }
ll unique_string(ll n ,ll size, char s[][size]){ ll ans=1; rep(i,1,n) if( strcmp(s[i],s[i-1]) ) ans++; return ans; }
ll unique_num(ll n , ll a[]){ ll ans=1; rep(i,1,n) if( a[i]!=a[i-1] ) ans++; return ans; }

ll compare(ll a, ll b){ return a<b?-1:a>b?1:0; }
typedef struct{ ll a , b;}fr;
int cmp1( const void *p, const void *q ){
    ll pa=((fr*)p)->a; ll qa=((fr*)q)->a;  return compare(pa,qa);
}
int cmp2( const void *p, const void *q ){
    ll pa=((fr*)p)->a; ll qa=((fr*)q)->a;  return compare(qa,pa);
}
void strsortup(fr*a,int n){qsort(a,n,sizeof(fr),cmp1);}
void strsortdown(fr*a,int n){qsort(a,n,sizeof(fr),cmp2);}

void sort_partial(ll a[],int begin,int end,int is_increase){
    ll *b;  b = (ll *) malloc( sizeof(ll)*(end-begin) );

    rep(i,begin,end) b[i-begin]=a[i];
    if(is_increase) sortup(b,end-begin); else sortdown(b,end-begin);
    rep(i,begin,end) a[i]=b[i-begin];
}

#pragma region AVL
/*---------------------------AVL start--------------------------------*/
//https://qiita.com/mikecat_mixc/items/e9f8248de2ae7f7a0a29
typedef struct node_AVL_set{
    ll val;
    int diff;
    int cnt;
    struct node_AVL_set *child[2];
}AVL_set;
void AVL_set_inside_rotate(AVL_set **node, int is_right){
    int l = is_right==false , r = is_right==true , sign = is_right ? 1 : -1;
    if((*node)->child[l] != NULL){
        AVL_set* left = (*node)->child[l];
        int a= (*node)->diff * sign , b= left->diff * sign ,na,nb;
        if(b+1){
            na=a-1-b;
            nb= (a-b) ? b-1 : a-2;
        }else{
            na=a-1;
            nb= a ? b-1 : a+b-2;
        }
        (*node)->diff = na * sign;
        left->diff = nb * sign;
        /* rotate-> */
        (*node)->child[l] = (*node)->child[l]->child[r];
        left->child[r] = *node;
        *node = left;
    }
}
int AVL_set_inside_update(AVL_set **node, ll data, int add){
    if(*node == NULL){
        if(add==2){
            *node = malloc(sizeof(AVL_set));
            (*node)->val = data;
            (*node)->cnt = 1;
            (*node)->diff = 0;
            (*node)->child[0] = NULL;
            (*node)->child[1] = NULL;
        }
        return add==2 ? *node != NULL : 0;
    }else{
        int l, delta, delta_sign; AVL_set *next_node;
        if(data == (*node)->val){
            if(add==2){
                (*node)->cnt++;
                return 0;
            }else{
                if( add && (*node)->cnt > 1 ){
                    (*node)->cnt--; return 0;
                }else{
                    if((*node)->child[1] == NULL){
                        next_node = (*node)->child[0];
                        free(*node);
                        *node = next_node;
                        return -1;
                    }else if((*node)->child[0] == NULL){
                        next_node = (*node)->child[1];
                        free(*node);
                        *node = next_node;
                        return -1;
                    }else{
                        for(next_node = (*node)->child[0];
                        next_node->child[1] != NULL;
                        next_node = next_node->child[1]);
                        (*node)->val = next_node->val;
                        l=0;
                        delta_sign=1;
                        delta = AVL_set_inside_update(&(*node)->child[l], next_node->val, add);
                    }
                }
            }
        }else{
            l = data >= (*node)->val ? 1 : 0;
            delta_sign = data < (*node)->val ? 1 : -1;
            delta = AVL_set_inside_update(&(*node)->child[l], data, add);
        }
        if( delta ){
            int orig_diff = (*node)->diff;
            int do_rotate = 0, rotate_l , diff_sign , rotate_right;
            (*node)->diff += delta * delta_sign;
            if((*node)->diff > 1){
                do_rotate = 1;
                rotate_l = 0;
                diff_sign = 1;
                rotate_right = 1;
            }else if((*node)->diff < -1){
                do_rotate = 1;
                rotate_l = 1;
                diff_sign = -1;
                rotate_right = 0;
            }
            if(do_rotate){
                int child_diff = (*node)->child[rotate_l]->diff;
                if((*node)->child[rotate_l]->diff * diff_sign < 0){
                    AVL_set_inside_rotate(&(*node)->child[rotate_l], !rotate_right);
                }
                AVL_set_inside_rotate(node, rotate_right);
                return delta < 0 && child_diff != 0 ? -1 : 0;
            }
            if     (delta > 0 && orig_diff == 0) return 1;
            else if(delta < 0 && orig_diff != 0) return -1;
            else                                 return 0;
        }else{
            return 0;
        }
    }
}

void AVL_set_inside_print(const AVL_set *node, int depth){
    if(node == NULL) return;
    AVL_set_inside_print(node->child[1], depth + 1);
    printf("%lld %d\n", node->val,node->cnt);
    AVL_set_inside_print(node->child[0], depth + 1);
}
void AVL_set_inside_free(AVL_set *node){
    if(node == NULL) return;
    AVL_set_inside_free(node->child[0]);
    AVL_set_inside_free(node->child[1]);
    free(node);
}
ll AVL_set_inside_count(AVL_set *root, ll val){
    AVL_set *node; node = root;
    while(node){
        if     (val < node->val) node = node->child[0];
        else if(val > node->val) node = node->child[1];
        else                     return node->cnt;
    }
    return 0;
}
ll AVL_set_inside_minmax(AVL_set *root, int type){
    while(root->child[type] !=NULL)  root= root->child[type];
    return root->val;
}
void AVL_set_inside_swap(AVL_set **node1, AVL_set **node2){
    AVL_set *tmp;  tmp=*node1; *node1=*node2; *node2=tmp;
}

#define set_MAX_SIZE 514511
ll set_main( int command , int set_num , ll val ){
    static bool set_is_init=false;
    static AVL_set *set_pointer[set_MAX_SIZE];
    static ll set_siz[set_MAX_SIZE];
    if(!set_is_init){ set_is_init=true; rep(i,0,set_MAX_SIZE){ *(set_pointer+i) = NULL; set_siz[i]=0; } }
    if(command==-1){ AVL_set_inside_print( set_pointer[set_num] ,0); }
    if(command==1){ AVL_set_inside_count(set_pointer[set_num],val) ? 1 : set_siz[set_num]++;  AVL_set_inside_update( &set_pointer[set_num] , val , 2 ); }
    if(command==2){ return AVL_set_inside_count(set_pointer[set_num],val); }
    if(command==3){ ( AVL_set_inside_count(set_pointer[set_num],val) > 1 ) ? 1 : set_siz[set_num]--;  AVL_set_inside_update( &set_pointer[set_num] , val,1); }
    if(command==4){ set_siz[set_num]--; AVL_set_inside_update( &set_pointer[set_num] , val , 0 ); }
    if(command==5){ set_siz[set_num]=0; AVL_set_inside_free( set_pointer[set_num] ); set_pointer[set_num] = NULL; }
    if(command==6){ return set_siz[set_num]; }
    if(command==7){ return AVL_set_inside_minmax(set_pointer[set_num],1); }
    if(command==8){ return AVL_set_inside_minmax(set_pointer[set_num],0); }
    if(command==9){ AVL_set_inside_swap(&set_pointer[set_num],&set_pointer[val]); }
    return 0;
}

void set_print(int set_num){ set_main(-1,set_num,0); }
void set_insert(int set_num, ll val){ set_main(1,set_num,val); }
ll set_count(int set_num, ll val){ return set_main(2,set_num,val); }
void set_erase(int set_num, ll val, int is_all){ if(is_all) set_main(4,set_num,val); else set_main(3,set_num,val); }
void set_clear(int set_num){ set_main(5,set_num,0); }
ll set_size(int set_num){ return set_main(6,set_num,0); }
ll set_max(int set_num){ return set_main(7,set_num,0); }
ll set_min(int set_num){ return set_main(8,set_num,0); }
void set_swap(ll set_num1, ll set_num2){ set_main(9,set_num1,set_num2); }

/*
insert *
size *
clear *
erase *
count *
max *
min *
swap *
begin
end
merge  source の要素のキーと等価なキーの要素がある場合、その要素は source から抽出されない
lower_bound
upper_bound
*/

// ll map_main()

/*---------------------------AVL start--------------------------------*/

#pragma endregion AVL

#pragma endregion kyopuro_templates

char s[1151154];
ll d[225225];
ll no[225225];
ll same[225225];
ll dif[225225];
void solve(){
    // fgets(s,sizeof(s),stdin);
    ll n;
    ll ans=0;
    cin(&n);
    // scanf("%s",s);
    ll a[n];
    ll cnt=0;
    rep(i,0,n){
        cin(&a[i]);
        if(!a[i]){
            no[i+1]=1;
        }else{
            d[a[i]]=1;
        }
    }
    // rep(i,1,n+1) printf("%lld ",no[i]); PN;
    // rep(i,1,n+1) printf("%lld ",d[i]); PN;
    rep(i,1,n+1){
        if(!d[i]&&no[i]){
            same[cnt++]=i;
            set_insert(0,i);
            set_insert(1,i);
        }else if(no[i]){
            set_insert(10,i);
        }else if(!d[i]){
            set_insert(11,i);
        }
    }
    // rep(i,0,cnt) printf("%lld ",same[i]); PN;
    // set_print(10);PN;
    // set_print(11);PN;
    if(cnt>1){
        rep(i,0,cnt-1){
            a[same[i]-1]=same[i+1];
        }
        a[same[cnt-1]-1]=same[0];
    }else if(cnt==1){
        ll x=same[0];
        ll y=set_max(11);
        a[x-1]=y;
        set_erase(11,y,1);

        x=same[0];
        y=set_max(10);
        a[y-1]=x;
        set_erase(10,y,1);
    }

    while(1){
        if(set_size(10)==0) break;
        ll x=set_min(10);
        ll y=set_max(11);
        a[x-1]=y;
        set_erase(10,x,1);
        set_erase(11,y,1);
    }
    rep(i,0,n){
        printf("%lld ",a[i]);
    }PN;
}

int main(void){
    ll T=1;
    // cin(&T);
    rep(i,0,T){ solve(); }
    return 0;
}

/*
    while (ng + 1 < ok) {
        ll p = ng + (ok - ng) / 2;
        ll cnt = 0;
        rep(i,0,n){
            cnt += (a[i] + p - 1) / p - 1;
        }
        if (cnt <= k) ok = p; else ng = p;
    }
*/
// 10^18 2^60 3^38 5^26


/* practice with Dukkha */
#include <stdio.h>

#define N	200000

int main() {
	static char used[N];
	static int pp[N], ii[N], jj[N];
	int n, m, h, i;

	scanf("%d", &n);
	m = 0;
	for (i = 0; i < n; i++) {
		int p;

		scanf("%d", &p), p--;
		pp[i] = p;
		if (p == -1)
			ii[m++] = i;
		else
			used[p] = 1;
	}
	m = 0;
	for (i = n - 1; i >= 0; i--)
		if (!used[i])
			jj[m++] = i;
	for (h = 0; h < m; h++)
		if (ii[h] == jj[h]) {
			int h_ = (h + 1) % m, tmp;

			tmp = jj[h], jj[h] = jj[h_], jj[h_] = tmp;
		}
	for (h = 0; h < m; h++)
		pp[ii[h]] = jj[h];
	for (i = 0; i < n; i++)
		printf("%d ", pp[i] + 1);
	printf("\n");
	return 0;
}


#include<stdio.h>

void merge (long long int x[100001], long long int s, long long int m, long long int e)
{
        long long int i,a[e-s+1],k,p=s,q=m+1;
        k=0;
        for (i=s;i<=e;i++)
        {
                if(p>m)
                a[k++]=x[q++];

                else if(q>e)
                a[k++]=x[p++];

                else if (x[p]<x[q])
                a[k++]=x[p++];

                else
                a[k++]=x[q++];
        }

        for (i=0;i<k;i++)
        x[s++]=a[i];
}

int mergesort(long long int a[10000],long long int s, long long int e)
{
        if (s<e)
        {
                long long int mid = (e+s)/2;
                mergesort (a,s,mid);
                mergesort (a,mid+1,e);

                merge (a,s,mid,e);
        }
}

int main () {
	long long int n,x[200004],f[200004],a[200004]={0},z,i,j,k;
	scanf("%lld",&n);
	for (i=0;i<n;i++)
	{
		scanf("%lld",&f[i]);
		if (f[i]!=0)
		a[f[i]]=1;
	}
	j=0;
	for (i=1;i<=n;i++)
	{
		if (a[i]==0)
		{
			x[j]=i;
			j++;
		}
	}
/*	for (i=0;i<j-1;i++)
	{
		k=x[i];
		x[i]=x[i+1];
		x[i+1]=k;
	}
	for (i=0;i<n;i++)
	{
		if (f[i]==0)
		{
			f[i]=x[z];
			z++;
		}
	}
*/
	z=0;
	for (i=0;i<n;i++)
	{
		if (f[i]==0)
		{
			f[i]=x[j-1];
			x[j-1]=x[z];
			z++;
		}
	}
	for (i=0;i<n;i++)
	printf("%lld ",f[i]);

	printf("\n");
}


#include<stdio.h>

void merge (long long int x[100001], long long int s, long long int m, long long int e)
{
        long long int i,a[e-s+1],k,p=s,q=m+1;
        k=0;
        for (i=s;i<=e;i++)
        {
                if(p>m)
                a[k++]=x[q++];

                else if(q>e)
                a[k++]=x[p++];

                else if (x[p]<x[q])
                a[k++]=x[p++];

                else
                a[k++]=x[q++];
        }

        for (i=0;i<k;i++)
        x[s++]=a[i];
}

int mergesort(long long int a[10000],long long int s, long long int e)
{
        if (s<e)
        {
                long long int mid = (e+s)/2;
                mergesort (a,s,mid);
                mergesort (a,mid+1,e);

                merge (a,s,mid,e);
        }
}

int main () {
	long long int n,x[200004],f[200004],a[200004]={0},z,i,j,k;
	scanf("%lld",&n);
	for (i=0;i<n;i++)
	{
		scanf("%lld",&f[i]);
		if (f[i]!=0)
		a[f[i]]=1;
	}
	j=0;
	for (i=1;i<=n;i++)
	{
		if (a[i]==0)
		{
			x[j]=i;
			j++;
		}
	}
	for (i=0;i<j-1;i++)
	{
		k=x[i];
		x[i]=x[i+1];
		x[i+1]=k;
	}
	z=0;
	for (i=0;i<n;i++)
	{
		if (f[i]==0)
		{
			f[i]=x[z];
			z++;
		}
	}
/*
	z=0;
	for (i=0;i<n;i++)
	{
		if (f[i]==0)
		{
			f[i]=x[j-1];
			x[j-1]=x[z];
			z++;
		}
	}
*/	for (i=0;i<n;i++)
	printf("%lld ",f[i]);

	printf("\n");
}


/* Coached by Dukkha and rainboy */
/* https://codeforces.com/contest/1283/submission/67841594 (rainboy) */
#include <stdio.h>

#define N 200000

int main() {
	static int ff[N], ii[N], jj[N];
	static int used[N + 1];
	int n, m, i, i_, f, tmp;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &ff[i]);
	m = 0;
	for (i = 0; i < n; i++) {
		f = ff[i];
		if (f == 0)
			ii[m++] = i;
		else
			used[f] = 1;
	}
	m = 0;
	for (f = n; f >= 1; f--)
		if (!used[f])
			jj[m++] = f;
	for (i = 0; i < m; i++)
		if (ii[i] == jj[i] - 1) {
			i_ = (i + 1) % m;
			tmp = jj[i], jj[i] = jj[i_], jj[i_] = tmp;
			break;
		}
	for (i = 0; i < m; i++)
		ff[ii[i]] = jj[i];
	for (i = 0; i < n; i++)
		printf("%d ", ff[i]);
	printf("\n");
	return 0;
}


/* Coached by Dukkha and rainboy */
/* https://codeforces.com/contest/1283/submission/67841594 (rainboy) */
#include <stdio.h>

#define N 200000

int main() {
	static int ff[N], ii[N], jj[N];
	static int used[N + 1];
	int n, m, i, i_, f, tmp;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &ff[i]);
	m = 0;
	for (i = 0; i < n; i++) {
		f = ff[i];
		if (f == 0)
			ii[m++] = i;
		else
			used[f] = 1;
	}
	m = 0;
	for (f = n; f >= 1; f--)
		if (!used[f])
			jj[m++] = f;
	for (i = 0; i < m; i++)
		if (ii[i] == jj[i]) {
			i_ = (i + 1) % m;
			tmp = jj[i], jj[i] = jj[i_], jj[i_] = tmp;
		}
	for (i = 0; i < m; i++)
		ff[ii[i]] = jj[i];
	for (i = 0; i < n; i++)
		printf("%d ", ff[i]);
	printf("\n");
	return 0;
}


//
//  main.c
//  5
//
//  Created by Дмитрий Голубев on 05.09.2020.
//  Copyright © 2020 Дмитрий Голубев. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int pos, next, prev, type;
}men;

typedef struct{
    int start, end;
}ring;

int main(int argc, const char * argv[]) {
    int n, k = 0, h = 0;
    scanf("%d", &n);
    men* ar = malloc(sizeof(men)*n);
    int* pos_h = malloc(sizeof(int)*n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &ar[i].next);
        ar[i].next--;
        ar[i].pos = i-1;
        ar[i].prev = -1;
    }
    for (int i = 0; i < n; i++) {
        if (ar[i].next != -1) ar[ar[i].next].prev = i;
    }
    for (int i = 0; i < n; i++) {
        if (ar[i].next == -1 && ar[i].prev == -1) {
            ar[i].type = 1;
            pos_h[k] = i;
            k++;
            h = i;
        }
        else if (ar[i].next != -1 && ar[i].prev == -1) {
            ar[i].type = 2;
        }
        else if (ar[i].next == -1 && ar[i].prev != -1) {
            ar[i].type = 3;
        }
        else{
            ar[i].type = 4;
        }
    }

    ring* r = malloc(sizeof(ring)*n);
    int r_i = 0;
    for (int i = 0; i < n; i++) {
        if(ar[i].type == 2){
            r[r_i].start = i;
            int j = i;
            while(ar[ar[j].next].type != 3 && ar[j].next != i){
                j = ar[j].next;
            }
            if(ar[j].next != i){
                r[r_i].end = ar[j].next;
                r_i++;
            }
        }
    }
    if(k == 1){
        ar[h].prev = r[0].end;
        ar[h].next = r[0].start;
        ar[r[0].end].next = h;
        ar[r[0].start].prev = h;

        for(int j = 1; j < r_i; j++){
            ar[r[j].end].next = r[j].start;
            ar[r[j].start].prev = r[j].end;
        }
    }
    else{
        for(int j = 0; j < r_i; j++){
            ar[r[j].end].next = r[j].start;
            ar[r[j].start].prev = r[j].end;
        }
        if (k%2 == 1) {
            ar[pos_h[k - 3]].next = pos_h[k - 2];
            ar[pos_h[k - 3]].prev = pos_h[k - 1];
            ar[pos_h[k - 2]].next = pos_h[k - 1];
            ar[pos_h[k - 2]].prev = pos_h[k - 3];
            ar[pos_h[k - 1]].next = pos_h[k - 3];
            ar[pos_h[k - 1]].prev = pos_h[k - 2];
            k-= 3;
        }
        for (int j = 0; j < k; j+= 2) {
            ar[pos_h[j]].next = pos_h[j+1];
            ar[pos_h[j]].prev = pos_h[j+1];
            ar[pos_h[j+1]].next = pos_h[j];
            ar[pos_h[j+1]].prev = pos_h[j];
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", ar[i].next+1);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int pos, next, prev, type;
}men;

typedef struct{
    int start, end;
}ring;

int main(int argc, const char * argv[]) {
    int n, k = 0, h = 0;
    scanf("%d", &n);
    men* ar = malloc(sizeof(men)*n);
    int* pos_h = malloc(sizeof(int)*n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &ar[i].next);
        ar[i].next--;
        ar[i].pos = i-1;
        ar[i].prev = -1;
    }
    for (int i = 0; i < n; i++) {
        if (ar[i].next != -1) ar[ar[i].next].prev = i;
    }
    for (int i = 0; i < n; i++) {
        if (ar[i].next == -1 && ar[i].prev == -1) {
            ar[i].type = 1;
            pos_h[k] = i;
            k++;
            h = i;
        }
        else if (ar[i].next != -1 && ar[i].prev == -1) {
            ar[i].type = 2;
        }
        else if (ar[i].next == -1 && ar[i].prev != -1) {
            ar[i].type = 3;
        }
        else{
            ar[i].type = 4;
        }
    }

    ring* r = malloc(sizeof(ring)*n);
    int r_i = 0;
    for (int i = 0; i < n; i++) {
        if(ar[i].type == 2){
            r[r_i].start = i;
            int j = i;
            while(ar[ar[j].next].type != 3 && ar[j].next != i){
                j = ar[j].next;
            }
            if(ar[j].next != i){
                r[r_i].end = ar[j].next;
                r_i++;
            }
        }
    }
    if(k == 1){
        ar[h].prev = r[0].end;
        ar[h].next = r[0].start;
        ar[r[0].end].next = h;
        ar[r[0].start].prev = h;

        for(int j = 1; j < r_i; j++){
            ar[r[j].end].next = r[j].start;
            ar[r[j].start].prev = r[j].end;
        }
    }
    else{
        for(int j = 0; j < r_i; j++){
            ar[r[j].end].next = r[j].start;
            ar[r[j].start].prev = r[j].end;
        }
        if (k%2 == 1) {
            ar[pos_h[0]].next = pos_h[1];
            ar[pos_h[0]].prev = pos_h[2];
            ar[pos_h[1]].next = pos_h[2];
            ar[pos_h[1]].prev = pos_h[0];
            ar[pos_h[2]].next = pos_h[0];
            ar[pos_h[2]].prev = pos_h[1];
            k-= 3;
        }
        for (int j = 0; j < k; j+= 2) {
            ar[pos_h[j]].next = pos_h[j+1];
            ar[pos_h[j]].prev = pos_h[j+1];
            ar[pos_h[j+1]].next = pos_h[j];
            ar[pos_h[j+1]].prev = pos_h[j];
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", ar[i].next+1);
    }

    return 0;
}


#include<stdio.h>
int a[600000];
int main()
{
	int n,ans=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]==0)
		{
			a[i]=ans;
			ans=i+1;
		}
	}
	for(int i=0;i<n;i++)
	{
		if(a[i]==0)
			printf("%d ",ans);
		else
			printf("%d ",a[i]);
	}
	printf("\n");
}


    # include <stdio.h>

    int main()
    {
    int n,count=0,c[200005];
    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
    int b[n+1];
    for(int i=0;i<n+1;i++)
    b[i]=0;
    for(int i=0;i<n;i++)
    b[a[i]]++;
    for(int i=1;i<n+1;i++)
    if(b[i]==0)
    c[count++]=i;
    count--;
    for(int i=0;i<n;i++)
    if(a[i]!=0)
    printf("%d ",a[i]);
    else{
    if(c[count]==i+1)
    {
    printf("%d ",c[count-1]);
    c[count-1]=c[count];
    count--;
    }
    else
    {
    printf("%d ",c[count]);
    count--;
    }
    }

    return 0;
    }


    # include <stdio.h>

    int main()
    {
    int n,count=0,c[200005],j=1,i,k;
    scanf("%d",&n);
    int a[n],p[n];
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
    int b[n+1];
    for(int i=0;i<n+1;i++)
    b[i]=0;
    for(int i=0;i<n;i++)
    b[a[i]]++;
for(int i=1;i<=n;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
{
if(a[i]!=0)
{
p[i]=a[i];
}
else
{

if(c[count]==(i+1) && count>0)
{
p[i]=c[count-1];
c[count-1]=c[count];
}
else
{
if(count==0 && c[count]==(i+1))
{
int temp=p[k];
p[i]=p[k];
p[k]=c[count];
//p[i]=p[k];
}
else
p[i]=c[count--];
}
k=i;
}
}
   for(int i=0;i<n;i++)
printf("%d ",p[i]);

    return 0;
    }


# include <stdio.h>

int main()
{
int n,count=0,c[200005];
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int b[n+1];
for(int i=0;i<n+1;i++)
b[i]=0;
for(int i=0;i<n;i++)
b[a[i]]++;
for(int i=1;i<n+1;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
if(a[i]!=0)
printf("%d ",a[i]);
else{
if(c[count]==i+1)
{
printf("%d ",c[count-1]);
c[count-1]=c[count];
count--;
}
else
{
printf("%d ",c[count]);
count--;
}
}

return 0;
}


    # include <stdio.h>

    int main()
    {
    int n,count=0,c[2000005],i,k;
    scanf("%d",&n);
    int a[n],p[n+10];
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
    int b[n+1];
    for(int i=0;i<n+1;i++)
    b[i]=0;
    for(int i=0;i<n;i++)
    b[a[i]]++;
for(int i=1;i<=n;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
{
if(a[i]!=0)
{
p[i]=a[i];
}
else
{

if(c[count]==(i+1) && count>0)
{
p[i]=c[count-1];
c[count-1]=c[count];
}
else
{
if(count==0 && c[count]==(i+1))
{
//int temp=p[k];
p[i]=p[k];
p[k]=c[count];
//p[i]=p[k];
}
else
p[i]=c[count--];
}
k=i;
}
}
   for(int i=0;i<n;i++)
printf("%d ",p[i]);

    return 0;
    }


    # include <stdio.h>

    int main()
    {
    int n,count=0,c[2000005],i,k;
    scanf("%d",&n);
    int a[n],p[n+10];
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
    int b[n+1];
    for(int i=0;i<n+1;i++)
    b[i]=0;
    for(int i=0;i<n;i++)
    b[a[i]]++;
for(int i=1;i<=n;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
{
if(a[i]!=0)
{
p[i]=a[i];
}
else
{

if(c[count]==(i+1) && count>0)
{
p[i]=c[count-1];
c[count-1]=c[count];
count--;
}
else
{
if(count==0 && c[count]==(i+1))
{
//int temp=p[k];
p[i]=p[k];
p[k]=c[count];
//p[i]=p[k];
}
else
p[i]=c[count--];
}
k=i;
}
}
   for(int i=0;i<n;i++)
printf("%d ",p[i]);

    return 0;
    }


# include <stdio.h>

int main()
{
int n,count=0,c[100000];
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int b[n+1];
for(int i=0;i<n+1;i++)
b[i]=0;
for(int i=0;i<n;i++)
b[a[i]]++;
for(int i=1;i<n+1;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
if(a[i]!=0)
printf("%d ",a[i]);
else{
if(c[count]==i+1)
{
printf("%d ",c[count-1]);
c[count-1]=c[count];
count--;
}
else
{
printf("%d ",c[count]);
count--;
}
}

return 0;
}


# include <stdio.h>

int main()
{
int n,count=0,c[100000];
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int b[n+1];
for(int i=0;i<n+1;i++)
b[i]=0;
for(int i=0;i<n;i++)
b[a[i]]++;
for(int i=1;i<n+1;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
if(a[i]!=0)
printf("%d ",a[i]);
else{
printf("%d ",c[count]);
count--;
}

return 0;
}


# include <stdio.h>

int main()
{
int n,count=0,c[100000];
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int b[n+1];
for(int i=0;i<n+1;i++)
b[i]=0;
for(int i=0;i<n;i++)
b[a[i]]++;
for(int i=1;i<n+1;i++)
if(b[i]==0)
c[count++]=i;
count--;
for(int i=0;i<n;i++)
if(a[i]!=0)
printf("%d ",a[i]);
else{
if(c[count]==i+1)
{
printf("%d ",c[count-1]);
c[count-1]=c[count];
}
printf("%d ",c[count]);
count--;
}

return 0;
}


#include <stdio.h>
int giftGiven[200005];
int giftRecievedChecker[200005];
int giftRecieved[200005];
int takeInput[200005];
int main(void)
{
	int inputLength;
	scanf("%d",&inputLength);
	int counter = 1;
	for(int i=0;i<200005;i++)
	{
		giftRecieved[i] = 0;
		giftRecievedChecker[i] =0;
		giftGiven[i] = 0;
		takeInput[i] = 0;
	}
	for(int i=1;i<=inputLength;i++)
	{
		scanf("%d",&takeInput[i]);
		giftRecievedChecker[takeInput[i]]++;
		if(takeInput[i] == 0)
		{
			giftGiven[counter] = i;
			counter++;
		}
	}
	counter = 1;
	for(int i=1;i<=inputLength;i++)
	{
		if(giftRecievedChecker[i] == 0)
		{
			giftRecieved[counter] = i;
			counter++;
		}
	}
//	printf("%d counter is \n", counter);
	for(int i=1;i<counter;i++)
	{
		if(i!=counter-1)
		{
			if(giftGiven[i] == giftRecieved[i])
			{
				int swap = giftRecieved[i];
				giftRecieved[i] = giftRecieved[i+1];
				giftRecieved[i+1] = swap;
			}
		}
		else
		{
			if(giftGiven[i] == giftRecieved[i])
			{
				int swap = giftRecieved[i];
				giftRecieved[i] = giftRecieved[i-1];
				giftRecieved[i-1] = swap;
			}
		}
	}
	counter = 1;
	for(int i=1;i<=inputLength;i++)
	{
		if(takeInput[i] != 0)
		{
			printf("%d ",takeInput[i]);
		}
		else
		{
			printf("%d ",giftRecieved[counter]);
			counter++;
		}
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
int giftGiven[200005];
int giftRecievedChecker[200005];
int giftRecieved[200005];
int takeInput[200005];
int main(void)
{
	int inputLength;
	scanf("%d",&inputLength);
	int counter = 1;
	for(int i=0;i<200005;i++)
	{
		giftRecieved[i] = 0;
		giftRecievedChecker[i] =0;
		giftGiven[i] = 0;
		takeInput[i] = 0;
	}
	for(int i=1;i<=inputLength;i++)
	{
		scanf("%d",&takeInput[i]);
		giftRecievedChecker[takeInput[i]]++;
		if(takeInput[i] == 0)
		{
			giftGiven[counter] = i;
			counter++;
		}
	}
	counter = 1;
	for(int i=1;i<=inputLength;i++)
	{
		if(giftRecievedChecker[i] == 0)
		{
			giftRecieved[counter] = i;
			counter++;
		}
	}
//	printf("%d counter is \n", counter);
	for(int i=1;i<counter;i++)
	{
		if(i!=counter-1)
		{
			if(giftGiven[i] == giftRecieved[i])
			{
				int swap = giftRecieved[i];
				giftRecieved[i] = giftRecieved[i+1];
				giftRecieved[i+1] = swap;
			}
		}
		else
		{
			int swap = giftRecieved[i];
			giftRecieved[i] = giftRecieved[i-1];
			giftRecieved[i-1] = swap;
		}
	}
	counter = 1;
	for(int i=1;i<=inputLength;i++)
	{
		if(takeInput[i] != 0)
		{
			printf("%d ",takeInput[i]);
		}
		else
		{
			printf("%d ",giftRecieved[counter]);
			counter++;
		}
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
int check[200005];
int give[200005];
int reciv[200005];
int main()
{
    int t,n=0;
    scanf("%d", &t);
    for (int i = 1; i <= t;i++){
        scanf("%d", &check[i]);
        if(check[i])
            give[i] =1;
        else
            n++;
        reciv[check[i]] = 1;
    }
    /*for (int i = 1; i <= t; i++)
            printf("%d ", reciv[i]);
    printf("\n");*/
    int pg, pr;
    pg = 1, pr =  t ;
    while(n--){
        while(give[pg])
            pg++;
        while(reciv[pr])
            pr--;
        if(pg!=pr){
            check[pg] = pr;
            give[pg] = 2;
            reciv[pr] = 1;
        }
        else{
            n++;
            pg++;
            pr--;

        }
    }
    for (int i = 1; i <= t;i++){
        pg = 1;
        if(check[i]==0){
            while(give[pg]!=2)
                pg++;
            check[i] = check[pg];
            check[pg] = i;
            give[i] = 2;
            reciv[i] = 1;
        }

    }

    for (int i = 1; i <= t; i++)
            printf("%d ", check[i]);
    /*printf("\n");
    for (int i = 1; i <= t; i++)
            printf("%d ", give[i]);
    printf("\n");
    for (int i = 1; i <= t;i++)
        printf("%d ", reciv[i]);*/
    return 0;
}


#include <stdio.h>
int check[20005];
int give[20005];
int reciv[20005];
int main()
{
    int t,n=0;
    scanf("%d", &t);
    for (int i = 1; i <= t;i++){
        scanf("%d", &check[i]);
        if(check[i])
            give[i] =1;
        else
            n++;
        reciv[check[i]] = 1;
    }
    /*for (int i = 1; i <= t; i++)
            printf("%d ", reciv[i]);
    printf("\n");*/
    int pg, pr;
    pg = 1, pr =  t ;
    while(n--){
        while(give[pg])
            pg++;
        while(reciv[pr])
            pr--;
        if(pg!=pr){
            check[pg] = pr;
            give[pg] = 2;
            reciv[pr] = 1;
        }
        else{
            n++;
            pg++;
            pr--;

        }
    }
    for (int i = 1; i <= t;i++){
        pg = 1;
        if(check[i]==0){
            while(give[pg]!=2)
                pg++;
            check[i] = check[pg];
            check[pg] = i;
            give[i] = 2;
            reciv[i] = 1;
        }

    }

    for (int i = 1; i <= t; i++)
            printf("%d ", check[i]);
    /*printf("\n");
    for (int i = 1; i <= t; i++)
            printf("%d ", give[i]);
    printf("\n");
    for (int i = 1; i <= t;i++)
        printf("%d ", reciv[i]);*/
    return 0;
}


    #include <stdio.h>
    #include <stdbool.h>
    int f[200005],mark[200005],unused[200005];
    int n;
	bool judge(){
    	int i;
    	for(i=1;i<=n;i++){
    		if(f[i]==i)
    			return false;
		}
		return true;
	}
	int main()
    {
        int i,j,left,right;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
        	scanf("%d",&f[i]);
        	mark[f[i]]++;
		}
		int ans=0;
		for(i=1;i<=n;i++){
			if(mark[i]==0){
				ans++;
				unused[ans]=i;
			}
		}
		left=1;
		right=ans;
		for(i=1;i<=n;i++){
			if(f[i]==0){
				if(i!=unused[right]){
					f[i]=unused[right];
					right--;
				}
				else{
					f[i]=unused[left];
					left++;
				}
			}
		}
		if(!judge()){
			for(i=1;i<=n;i++){
				if(f[i]==0){
					if(i!=unused[left]){
						f[i]=unused[left];
						left++;
					}
					else{
						f[i]=unused[right];
						right--;
					}
				}
			}
		}
		for(i=1;i<=n;i++)
			printf("%d ",f[i]);
    }


    #include <stdio.h>
    #include <stdbool.h>
    int f[200005],mark[200005],unused[200005],b[200005];
    int n;
	bool judge(){
    	int i;
    	for(i=1;i<=n;i++){
    		if(b[i]==i)
    			return false;
		}
		return true;
	}
	int main()
    {
        int i,j,left,right;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
        	scanf("%d",&f[i]);
        	mark[f[i]]++;
		}
		int ans=0;
		for(i=1;i<=n;i++){
			if(mark[i]==0){
				ans++;
				unused[ans]=i;
			}
		}
		left=1;
		right=ans;
		for(i=1;i<=n;i++){
			if(f[i]!=0)
				b[i]=f[i];
			else{
				if(i!=unused[right]){
					b[i]=unused[right];
					right--;
				}
				else{
					b[i]=unused[left];
					left++;
				}
			}
		}
		if(!judge()){
			left=1;
			right=ans;
			for(i=1;i<=n;i++){
				if(f[i]!=0)
					b[i]=f[i];
				else{
					if(i!=unused[left]){
						b[i]=unused[left];
						left++;
					}
					else{
						b[i]=unused[right];
						right--;
					}
				}
			}
		}
		for(i=1;i<=n;i++)
			printf("%d ",b[i]);
    }


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    int zeros[d];

    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            zeros[pos]=i;
            pos=pos+1;
        }else{
            pos2=pos2+1;
        }
    }
    int nonzeros[pos2];
    pos2=0;
    for(int i=0; i<d; i++){
        if(arr[i]!=0){
            nonzeros[pos2]=arr[i];
            pos2=pos2+1;
        }
    }
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    for(int i=1; i<nonzeros[0]; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    for(int i=0; i<pos2; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    for(int i=nonzeros[pos2-1]+1; i<=d; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
        }else{
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        arr[zeros[pos-3]]=index[pos-2];
        arr[zeros[pos-2]]=index[pos-1];
        arr[zeros[pos-1]]=index[pos-3];
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    int zeros[d];

    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            zeros[pos]=i;
            pos=pos+1;
        }else{
            pos2=pos2+1;
        }
    }
    int nonzeros[2];
    pos2=0;
    for(int i=0; i<d; i++){
        if(arr[i]!=0){
            nonzeros[pos2]=arr[i];
            pos2=pos2+1;
        }
    }
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    for(int i=1; i<nonzeros[0]; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    for(int i=0; i<pos2; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    for(int i=nonzeros[pos2-1]+1; i<=d; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
        }else{
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        arr[zeros[pos-3]]=index[pos-2];
        arr[zeros[pos-2]]=index[pos-1];
        arr[zeros[pos-1]]=index[pos-3];
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    int zeros[d];
    int nonzeros[2];
    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            zeros[pos]=i;
            pos=pos+1;
        }else{/////////////
            nonzeros[pos2]=temp;
            pos2=pos2+1;
        }
    }
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    for(int i=1; i<nonzeros[0]; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    for(int i=0; i<pos2; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    for(int i=nonzeros[pos2-1]+1; i<=d; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
        }else{
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        arr[zeros[pos-3]]=index[pos-2];
        arr[zeros[pos-2]]=index[pos-1];
        arr[zeros[pos-1]]=index[pos-3];
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];

    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            pos=pos+1;
        }else{
            pos2=pos2+1;
        }
    }

    int zeros[pos];
    int nonzeros[pos2];
    //printf("%d\n", pos2);
    pos2=0;
    pos=0;
    for(int i=0; i<d; i++){
        if(arr[i]!=0){
            nonzeros[pos2]=arr[i];
            pos2=pos2+1;
        }else{
            zeros[pos]=i;
            pos=pos+1;
        }
    }
    if(pos2!=0)
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    if(pos2!=0){
        for(int i=1; i<nonzeros[0]; i++){
            index[pos3]=i;
            pos3=pos3+1;
        }
    }
    for(int i=0; i<pos2-1; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    if(pos2!=0){
        for(int i=nonzeros[pos2-1]+1; i<=d; i++){
            index[pos3]=i;
            pos3=pos3+1;
        }
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                if(zeros[i]==index[i+1]-1 || zeros[i+1]==index[i]-1){
                    arr[zeros[i+1]]=index[i+1];
                    arr[zeros[i]]=index[i];
                }else{
                    arr[zeros[i]]=index[i+1];
                    arr[zeros[i+1]]=index[i];
                }
                i++;
            }
        }else{
            for(int i=0; i<pos-1; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        if(pos!=d){
            for(int i=0; i<pos-3; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
            if(zeros[pos-3]==index[pos-2]-1 || zeros[pos-2]==index[pos-1]-1 || zeros[pos-1]==index[pos-3]-1){
                arr[zeros[pos-3]]=index[pos-3];
                if(zeros[pos-2]==index[pos-2]-1 || zeros[pos-1]==index[pos-1]-1){
                    arr[zeros[pos-1]]=index[pos-2];
                    arr[zeros[pos-2]]=index[pos-1];
                }else{
                    arr[zeros[pos-2]]=index[pos-2];
                    arr[zeros[pos-1]]=index[pos-1];
                }
            }else{
                arr[zeros[pos-3]]=index[pos-2];
                arr[zeros[pos-2]]=index[pos-1];
                arr[zeros[pos-1]]=index[pos-3];
            }
        }else{
            for(int i=0; i<pos-3; i++){
                arr[zeros[i]]=i+2;
                arr[zeros[i+1]]=i+1;
                i++;
            }
            arr[zeros[pos-3]]=pos-2+1;
            arr[zeros[pos-2]]=pos-1+1;
            arr[zeros[pos-1]]=pos-3+1;
        }
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}
int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    int zeros[d];
    int nonzeros[d];
    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            zeros[pos]=i;
            pos=pos+1;
        }else{
            nonzeros[pos2]=temp;
            pos2=pos2+1;
        }
    }
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    for(int i=1; i<nonzeros[0]; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    for(int i=0; i<pos2; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    for(int i=nonzeros[pos2-1]+1; i<=d; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
        }else{
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        arr[zeros[pos-3]]=index[pos-2];
        arr[zeros[pos-2]]=index[pos-1];
        arr[zeros[pos-1]]=index[pos-3];
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}
int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    int zeros[d];
    int nonzeros[d];
    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            zeros[pos]=i;
            pos=pos+1;
        }else{
            nonzeros[pos2]=temp;
            pos2=pos2+1;
        }
    }
    qsort((void*)nonzeros, pos2, sizeof(int), cmpfunc);
    int index[pos];
    int pos3=0;
    for(int i=1; i<nonzeros[0]; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    for(int i=0; i<pos2; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    for(int i=nonzeros[pos2-1]+1; i<=d; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
        }else{
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        arr[zeros[pos-3]]=index[pos-2];
        arr[zeros[pos-2]]=index[pos-1];
        arr[zeros[pos-1]]=index[pos-3];
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];

    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            pos=pos+1;
        }else{
            pos2=pos2+1;
        }
    }

    int zeros[pos];
    int nonzeros[pos2];
    //printf("%d\n", pos2);
    pos2=0;
    pos=0;
    for(int i=0; i<d; i++){
        if(arr[i]!=0){
            nonzeros[pos2]=arr[i];
            pos2=pos2+1;
        }else{
            zeros[pos]=i;
            pos=pos+1;
        }
    }
    if(pos2!=0)
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    if(pos2!=0){
        for(int i=1; i<nonzeros[0]; i++){
            index[pos3]=i;
            pos3=pos3+1;
        }
    }
    for(int i=0; i<pos2-1; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    if(pos2!=0){
        for(int i=nonzeros[pos2-1]+1; i<=d; i++){
            index[pos3]=i;
            pos3=pos3+1;
        }
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                if(zeros[i]==index[i+1]-1 || zeros[i+1]==index[i]-1){
                    arr[zeros[i+1]]=index[i+1];
                    arr[zeros[i]]=index[i];
                }else{
                    arr[zeros[i]]=index[i+1];
                    arr[zeros[i+1]]=index[i];
                }
                i++;
            }
        }else{
            for(int i=0; i<pos-1; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        if(zeros[pos-3]==index[pos-2]-1 || zeros[pos-2]==index[pos-1]-1 || zeros[pos-1]==index[pos-3]-1){
            arr[zeros[pos-3]]=index[pos-3];
            arr[zeros[pos-2]]=index[pos-2];
            arr[zeros[pos-1]]=index[pos-1];
        }else{
            arr[zeros[pos-3]]=index[pos-2];
            arr[zeros[pos-2]]=index[pos-1];
            arr[zeros[pos-1]]=index[pos-3];
        }
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];

    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            pos=pos+1;
        }else{
            pos2=pos2+1;
        }
    }

    int zeros[pos];
    int nonzeros[pos2];
    //printf("%d\n", pos2);
    pos2=0;
    pos=0;
    for(int i=0; i<d; i++){
        if(arr[i]!=0){
            nonzeros[pos2]=arr[i];
            pos2=pos2+1;
        }else{
            zeros[pos]=i;
            pos=pos+1;
        }
    }
    if(pos2!=0)
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    if(pos2!=0){
        for(int i=1; i<nonzeros[0]; i++){
            index[pos3]=i;
            pos3=pos3+1;
        }
    }
    for(int i=0; i<pos2-1; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    if(pos2!=0){
        for(int i=nonzeros[pos2-1]+1; i<=d; i++){
            index[pos3]=i;
            pos3=pos3+1;
        }
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                if(zeros[i]==index[i+1]-1 || zeros[i+1]==index[i]-1){
                    arr[zeros[i+1]]=index[i+1];
                    arr[zeros[i]]=index[i];
                }else{
                    arr[zeros[i]]=index[i+1];
                    arr[zeros[i+1]]=index[i];
                }
                i++;
            }
        }else{
            for(int i=0; i<pos-1; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        if(pos!=d){
            for(int i=0; i<pos-3; i++){
                if(zeros[i]==index[i+1]-1 || zeros[i+1]==index[i]-1){
                    arr[zeros[i+1]]=index[i+1];
                    arr[zeros[i]]=index[i];
                }else{
                    arr[zeros[i]]=index[i+1];
                    arr[zeros[i+1]]=index[i];
                }
                i++;
            }
            if(zeros[pos-3]==index[pos-2]-1 || zeros[pos-2]==index[pos-1]-1 || zeros[pos-1]==index[pos-3]-1){
                arr[zeros[pos-3]]=index[pos-3];
                if(zeros[pos-2]==index[pos-2]-1 || zeros[pos-1]==index[pos-1]-1){
                    arr[zeros[pos-1]]=index[pos-2];
                    arr[zeros[pos-2]]=index[pos-1];
                }else{
                    arr[zeros[pos-2]]=index[pos-2];
                    arr[zeros[pos-1]]=index[pos-1];
                }
                if(zeros[pos-3]==index[pos-3]-1){
                    arr[zeros[pos-3]]=index[pos-2];
                    arr[zeros[pos-2]]=index[pos-3];
                }
            }else{
                arr[zeros[pos-3]]=index[pos-2];
                arr[zeros[pos-2]]=index[pos-1];
                arr[zeros[pos-1]]=index[pos-3];
            }
        }else{
            for(int i=0; i<pos-3; i++){
                arr[zeros[i]]=i+2;
                arr[zeros[i+1]]=i+1;
                i++;
            }
            arr[zeros[pos-3]]=pos-2+1;
            arr[zeros[pos-2]]=pos-1+1;
            arr[zeros[pos-1]]=pos-3+1;
        }
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

int compare(const void* v1, const void* v2){
    return (*(int*)v1-*(int*)v2);
}

int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    int zeros[d];
    int nonzeros[2];
    int pos2=0;
    int pos=0;
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
        if(temp==0){
            zeros[pos]=i;
            pos=pos+1;
        }else{
            nonzeros[pos2]=temp;
            pos2=pos2+1;
        }
    }
    qsort(&nonzeros, pos2, sizeof(int), compare);
    int index[pos];
    int pos3=0;
    for(int i=1; i<nonzeros[0]; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    for(int i=0; i<pos2; i++){
        for(int j=nonzeros[i]+1; j<=nonzeros[i+1]-1; j++){
            index[pos3]=j;
            pos3=pos3+1;
        }
    }
    for(int i=nonzeros[pos2-1]+1; i<=d; i++){
        index[pos3]=i;
        pos3=pos3+1;
    }
    if(pos%2==0){
        if(pos!=d){
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=index[i+1];
                arr[zeros[i+1]]=index[i];
                i++;
            }
        }else{
            for(int i=0; i<pos; i++){
                arr[zeros[i]]=zeros[i+1]+1;
                arr[zeros[i+1]]=zeros[i]+1;
                i++;
            }
        }

    }else{
        for(int i=0; i<pos-3; i++){
            arr[zeros[i]]=index[i+1];
            arr[zeros[i+1]]=index[i];
            i++;
        }
        arr[zeros[pos-3]]=index[pos-2];
        arr[zeros[pos-2]]=index[pos-1];
        arr[zeros[pos-1]]=index[pos-3];
    }
    for(int i=0; i<d; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}
/*int main(){
    int d;
    scanf("%d", &d);
    int arr[d];
    for(int i=0; i<d; i++){
        int temp;
        scanf("%d", &temp);
        arr[i]=temp;
    }
    qsort(arr, d, 4, cmpfunc);
    return 0;
}*/


import java.util.*;
import java.io.*;
public class Programa {
	public static boolean is(String s)
	{
		for(int i = 0, j = s.length() - 1; i < s.length(); i++, j--)
			if(s.charAt(i) != s.charAt(j))
				return false;
		return true;
	}

	public static int consecutive(int [] v){
		int maxi = 0;
		for(int i = 0; i < v.length; i++){
			int cont = 0;

			if(v[i] == 1) cont++;
			while(i + 1 < v.length && v[i] == 1 && v[i] == v[i + 1]){
				cont++;
				i++;

			}

			if(maxi < cont)
				maxi = cont;
		}
		return maxi;
	}
	public static String girar(String s){
		char [] v = s.toCharArray();

		char c = v[0];
		for(int j = 1; j < v.length; j++){
			v[j-1] = v[j];
		}
		v[v.length-1] = c;

		String g = "";
    	for(int i =0 ; i < v.length; i++)
    		g += v[i];
    	return g;
	}

	public static String convert(char [] v){
		String s = "";
		for(int i = 0; i < v.length; i++)
			s += v[i];
		return s;
	}

	public static String bestmod(String s, int k)
	{
		String mini = "";
		char [] a= s.toCharArray();
		for(int j = 0; j < k; j++)
			a[j] = 'a';
		mini = convert(a);

		for(int i = 0; i < s.length(); i++){
			String temp = girar(s);

			char [] v= temp.toCharArray();
			int r = 0;
			for(int j = 0; j < k; j++){
				while(r < s.length() && v[r] == 'a'){
					r++;
					continue;
				}
				if(r < s.length())v[r] = 'a';
			}
			String b = convert(v);

			if(mini.compareTo(b) > 0)
				mini = b;
			s = temp;

		}

		return mini;
	}

    public static void main(String [] args){
    	Scanner scan = new Scanner (System.in);
    	char [] v = scan.next().toCharArray();
    	int conta = 0, contb = 0;
    	for(int i = 0; i < v.length - 1; i++){
    		if(conta < 1 && v[i] == 'A' && v[i+1] == 'B'){
    			conta++;
    			i++;
    			continue;
    		}

    		if(contb < 1 && v[i] == 'B' && v[i+1] == 'A'){
    			contb++;
    			i++;
    			continue;
    		}
    	}
    	//AAABABAx|x|x||
    	//BABABAA

    	if(conta > 0 && contb > 0)
    		System.out.println("YES");
    	else
    		System.out.println("NO");
    }

    /*

     */
}


import java.util.*;
import java.io.*;
public class Programa {
	public static boolean is(String s)
	{
		for(int i = 0, j = s.length() - 1; i < s.length(); i++, j--)
			if(s.charAt(i) != s.charAt(j))
				return false;
		return true;
	}

	public static int consecutive(int [] v){
		int maxi = 0;
		for(int i = 0; i < v.length; i++){
			int cont = 0;

			if(v[i] == 1) cont++;
			while(i + 1 < v.length && v[i] == 1 && v[i] == v[i + 1]){
				cont++;
				i++;

			}

			if(maxi < cont)
				maxi = cont;
		}
		return maxi;
	}
	public static String girar(String s){
		char [] v = s.toCharArray();

		char c = v[0];
		for(int j = 1; j < v.length; j++){
			v[j-1] = v[j];
		}
		v[v.length-1] = c;

		String g = "";
    	for(int i =0 ; i < v.length; i++)
    		g += v[i];
    	return g;
	}

	public static String convert(char [] v){
		String s = "";
		for(int i = 0; i < v.length; i++)
			s += v[i];
		return s;
	}

	public static String bestmod(String s, int k)
	{
		String mini = "";
		char [] a= s.toCharArray();
		for(int j = 0; j < k; j++)
			a[j] = 'a';
		mini = convert(a);

		for(int i = 0; i < s.length(); i++){
			String temp = girar(s);

			char [] v= temp.toCharArray();
			int r = 0;
			for(int j = 0; j < k; j++){
				while(r < s.length() && v[r] == 'a'){
					r++;
					continue;
				}
				if(r < s.length())v[r] = 'a';
			}
			String b = convert(v);

			if(mini.compareTo(b) > 0)
				mini = b;
			s = temp;

		}

		return mini;
	}

    public static void main(String [] args){
    	Scanner scan = new Scanner (System.in);
    	char [] v = scan.next().toCharArray();
    	boolean [] f = {false, false,false};
    	int cont = 0, contb = 0;
    	for(int i = 0; i < v.length - 1; i++){
    		if(v[i] == 'A' && v[i+1] == 'B'){
    			f[0] = true;
    			v[i] = '?';v[i+1] = '?';

    			if(!f[2])
    				cont +=1;
    			else
				{System.out.println("NO");System.exit(0);}
    		}
    		if(v[i] == 'B' && v[i+1] == 'A'){
    			f[1] = true;
    			v[i] = '?';v[i+1] = '?';
    			if(!f[2])
    				cont +=1;
    			else
				{System.out.println("NO");System.exit(0);}
    		}
    		if(f[0] && f[1])
    			f[2] = true;
    	}
    	if(f[0] && f[1])
    		System.out.println("YES");
    	else
    		System.out.println("NO");
    }

    /*

     */
}


import java.util.*;
import java.io.*;
public class Programa {
	public static boolean is(String s)
	{
		for(int i = 0, j = s.length() - 1; i < s.length(); i++, j--)
			if(s.charAt(i) != s.charAt(j))
				return false;
		return true;
	}

	public static int consecutive(int [] v){
		int maxi = 0;
		for(int i = 0; i < v.length; i++){
			int cont = 0;

			if(v[i] == 1) cont++;
			while(i + 1 < v.length && v[i] == 1 && v[i] == v[i + 1]){
				cont++;
				i++;

			}

			if(maxi < cont)
				maxi = cont;
		}
		return maxi;
	}
	public static String girar(String s){
		char [] v = s.toCharArray();

		char c = v[0];
		for(int j = 1; j < v.length; j++){
			v[j-1] = v[j];
		}
		v[v.length-1] = c;

		String g = "";
    	for(int i =0 ; i < v.length; i++)
    		g += v[i];
    	return g;
	}

	public static String convert(char [] v){
		String s = "";
		for(int i = 0; i < v.length; i++)
			s += v[i];
		return s;
	}

	public static String bestmod(String s, int k)
	{
		String mini = "";
		char [] a= s.toCharArray();
		for(int j = 0; j < k; j++)
			a[j] = 'a';
		mini = convert(a);

		for(int i = 0; i < s.length(); i++){
			String temp = girar(s);

			char [] v= temp.toCharArray();
			int r = 0;
			for(int j = 0; j < k; j++){
				while(r < s.length() && v[r] == 'a'){
					r++;
					continue;
				}
				if(r < s.length())v[r] = 'a';
			}
			String b = convert(v);

			if(mini.compareTo(b) > 0)
				mini = b;
			s = temp;

		}

		return mini;
	}

    public static void main(String [] args){
    	Scanner scan = new Scanner (System.in);
    	char [] v = scan.next().toCharArray();
    	boolean [] f = {false, false};
    	for(int i = 0; i < v.length - 1; i++){
    		if(v[i] == 'A' && v[i+1] == 'B'){
    			f[0] = true;
    			v[i] = '?';v[i+1] = '?';
    		}
    		if(v[i] == 'B' && v[i+1] == 'A'){
    			f[1] = true;
    			v[i] = '?';v[i+1] = '?';
    		}
    	}
    	if(f[0] && f[1])
    		System.out.println("YES");
    	else
    		System.out.println("NO");
    }

    /*

     */
}


import java.util.*;
import java.io.*;
public class Programa {
	public static boolean is(String s)
	{
		for(int i = 0, j = s.length() - 1; i < s.length(); i++, j--)
			if(s.charAt(i) != s.charAt(j))
				return false;
		return true;
	}

	public static int consecutive(int [] v){
		int maxi = 0;
		for(int i = 0; i < v.length; i++){
			int cont = 0;

			if(v[i] == 1) cont++;
			while(i + 1 < v.length && v[i] == 1 && v[i] == v[i + 1]){
				cont++;
				i++;

			}

			if(maxi < cont)
				maxi = cont;
		}
		return maxi;
	}
	public static String girar(String s){
		char [] v = s.toCharArray();

		char c = v[0];
		for(int j = 1; j < v.length; j++){
			v[j-1] = v[j];
		}
		v[v.length-1] = c;

		String g = "";
    	for(int i =0 ; i < v.length; i++)
    		g += v[i];
    	return g;
	}

	public static String convert(char [] v){
		String s = "";
		for(int i = 0; i < v.length; i++)
			s += v[i];
		return s;
	}

	public static String bestmod(String s, int k)
	{
		String mini = "";
		char [] a= s.toCharArray();
		for(int j = 0; j < k; j++)
			a[j] = 'a';
		mini = convert(a);

		for(int i = 0; i < s.length(); i++){
			String temp = girar(s);

			char [] v= temp.toCharArray();
			int r = 0;
			for(int j = 0; j < k; j++){
				while(r < s.length() && v[r] == 'a'){
					r++;
					continue;
				}
				if(r < s.length())v[r] = 'a';
			}
			String b = convert(v);

			if(mini.compareTo(b) > 0)
				mini = b;
			s = temp;

		}

		return mini;
	}

    public static void main(String [] args){
    	Scanner scan = new Scanner (System.in);
    	char [] v = scan.next().toCharArray();
    	int conta = 0, contb = 0;
    	for(int i = 0; i < v.length - 1; i++){
    		if(conta < 1 && v[i] == 'A' && v[i+1] == 'B'){
    			conta++;
    			i++;
    			continue;
    		}

    		if(conta > 0 && v[i] == 'B' && v[i+1] == 'A'){
    			contb++;
    			i++;

    		}
    		if(conta > 0 && contb > 0){
        		System.out.println("YES");
    			System.exit(0);
    		}//BABBBBBA
    	}
    	conta = 0; contb = 0;
    	for(int i = 0; i < v.length - 1; i++){
    		if(contb < 1 && v[i] == 'B' && v[i+1] == 'A'){
    			contb++;
    			i++;
    			continue;
    		}

    		if(contb > 0 && v[i] == 'A' && v[i+1] == 'B'){
    			conta++;
    			i++;

    		}
    		if(conta > 0 && contb > 0){
        		System.out.println("YES");
        		System.exit(0);
    		}
    	}
    	//AAABABAx|x|x||
    	//BABABAA

    	System.out.println("NO");
    }

    /*

     */
}