PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000162. Hoán vị huyền bí (Mystic Permutation)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hoán vị huyền bí (Mystic Permutation)

Mystic Permutation

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a permutation pp of 1,2,…,n1,2,\ldots,n, find the lexicographically smallest permutation qq satisfying qi≠piq_i\ne p_i for every index ii. If none exists, output -1.

Input

The first line contains nn. The second contains a permutation p1,…,pnp_1,\ldots,p_n.

Output

Print -1 if impossible; otherwise print the lexicographically smallest permutation qq avoiding pip_i at every position.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; pp là hoán vị của 1,2,…,n1,2,\ldots,n.
  • Subtask 2 (30 points): 1≤n≤1001\le n\le 100; pp là hoán vị của 1,2,…,n1,2,\ldots,n.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; pp là hoán vị của 1,2,…,n1,2,\ldots,n.

Examples

Input

4
2 3 1 4

Output

1 2 4 3

Explanation

The smallest valid permutation is [1,2,4,3][1,2,4,3]. Each of its four positions differs from p=[2,3,1,4]p=[2,3,1,4]. Keeping the smallest possible prefix [1,2][1,2], choosing 33 next would force the forbidden value 44 at the last position, so 4,34,3 is required.