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 LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local mma = math.max
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n")
local t = {}
local ng = {}
for i = 1, n + 1 do ng[i] = {} end
for i = 1, n do
  t[i] = io.read("*n")
  ng[t[i]][i] = true
end
t[n + 1] = n + 1

if n == 2 and t[1] == 2 then
  print(-1)
  os.exit()
end

local use = {}
for i = 1, n do use[i] = false end
use[n + 1] = true
local function merge(a, b)
  if use[a] then return b end
  if use[b] then return a end
  return mmi(a, b)
end
local st = SegTree.new(n, merge, n + 1)
for i = 1, n do
  st:setValue(i, i, true)
end
st:updateAll()
local ret = {}
local prv = n + 1
for i = 1, n - 3 do
  local left = 1
  while true do
    local v = st:getRange(left, n)
    if t[prv] == v then
      left = v + 1
    else
      table.insert(ret, v)
      use[v] = true
      st:setValue(v, v)
      prv = v
      break
    end
  end
end
local rem = {}
for i = 1, n do
  if not use[i] then table.insert(rem, i) end
end
-- #rem should be 3
do
  local a, b, c = rem[1], rem[2], rem[3]
  local af, bf, cf = true, true, true
  if t[prv] == a then af = false end
  if t[prv] == b then bf = false end
  if t[b] == c and t[c] == b then af = false end
  if t[a] == c and t[c] == a then bf = false end
  if af then
    table.insert(ret, a)
    if t[b] == c then
      table.insert(ret, c)
      table.insert(ret, b)
    else
      table.insert(ret, b)
      table.insert(ret, c)
    end
  elseif bf then
    table.insert(ret, b)
    if t[a] == c then
      table.insert(ret, c)
      table.insert(ret, a)
    else
      table.insert(ret, a)
      table.insert(ret, c)
    end
  else
    table.insert(ret, c)
    if t[a] == b then
      table.insert(ret, b)
      table.insert(ret, a)
    else
      table.insert(ret, a)
      table.insert(ret, b)
    end
  end
end

print(table.concat(ret, " "))