Problem:
Score : 800 points

Problem Statement

Niwango has N cards, numbered 1,2,\ldots,N.
He will now arrange these cards in a row.

Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below.
To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.

To the immediate right of Card 1 (if any) is NOT Card a_1.

To the immediate right of Card 2 (if any) is NOT Card a_2.

\vdots

To the immediate right of Card N (if any) is NOT Card a_N.

Constraints

2 \leq N \leq 10^{5}

1 \leq a_i \leq N

a_i \neq i

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

If no arrangements satisfy the conditions, print -1. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:

b_1 b_2 \ldots b_N

Here, b_i represents the i-th card from the left.

Sample Input 1

4
2 3 4 1

Sample Output 1

1 3 2 4

The arrangement (1,2,3,4) is lexicographically smaller than (1,3,2,4), but is invalid, since it violates the condition "to the immediate right of Card 1 is not Card 2."

Sample Input 2

2
2 1

Sample Output 2

-1

If no arrangements satisfy the conditions, print -1.

Sample Input 3

13
2 3 4 5 6 7 8 9 10 11 12 13 12

Sample Output 3

1 3 2 4 6 5 7 9 8 10 12 11 13

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[test]
fn test_solve() {
    assert_eq!(solve("4\n2 3 4 1\n"), "1 3 2 4\n");
    assert_eq!(solve("2\n2 1\n"), "-1\n");
    assert_eq!(
        solve("13\n2 3 4 5 6 7 8 9 10 11 12 13 12\n"),
        "1 3 2 4 6 5 7 9 8 10 12 11 13\n"
    );
}

fn solve(src: &str) -> String {
    let mut sc = Scanner::from_str(src);

    let n: usize = sc.read();
    let a: Vec<usize> = (0..n).map(|_| sc.read()).collect();

    let mut p: Vec<_> = (1..n + 1).collect();
    let mut ip: Vec<_> = (0..n).collect();
    let mut swapped = vec![false; n-1];
    let pswap = |p: &mut Vec<_>, ip: &mut Vec<_>, i, j| {
        p.swap(i, j);
        ip[p[i]-1] = i;
        ip[p[j]-1] = j;
        return a[p[i]-1] == p[j];
    };


    for i in 0..n {
        let mut i = p[i]-1;
        while ip[i] < n -1 && p[ip[i] + 1] == a[i] {
            let ipi = ip[i];
            if ipi < n - 2 && !swapped[ipi+1] {
                if pswap(&mut p, &mut ip, ipi+1, ipi+2) {
                    return "-1\n".to_string()
                }
                i = ipi + 1;
            } else {
                if pswap(&mut p, &mut ip, ipi, ipi+1) {
                    return "-1\n".to_string()
                }
                swapped[ipi] = true;
                if ipi == 0 {
                    break;
                }
                i = ipi - 1;
            }
        }
    }

    let mut ret = String::new();
    use std::fmt::Write;
    
    for p in p {
        write!(&mut ret, "{} ", p).unwrap();
    }
    ret.pop();
    ret.push('\n');
    
    ret
}

fn main() {
    let mut buf = String::new();
    use std::io;
    use std::io::{Read, Write};
    io::stdin().read_to_string(&mut buf).unwrap();
    io::stdout().write_all(solve(&buf).as_bytes()).unwrap();
}

struct Scanner<'a>(std::str::SplitWhitespace<'a>);
impl<'a> Scanner<'a> {
    fn from_str(src: &str) -> Scanner {
        Scanner(src.split_whitespace())
    }
    fn get(&mut self) -> &str {
        self.0.next().unwrap()
    }
    fn read<T: std::str::FromStr>(&mut self) -> T {
        self.get().parse().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.get().chars().collect()
    }
}