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

#DSU0000036. Artsem và Saunders (Artsem and Saunders)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Artsem và Saunders (Artsem and Saunders)

Artsem and Saunders

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a function ff from [n]={1,2,…,n}[n]=\{1,2,\ldots,n\} to itself, represented by the sequence f(1),f(2),…,f(n)f(1),f(2),\ldots,f(n).

Find a positive integer mm and two functions g:[n]→[m]g:[n]\to[m] and h:[m]→[n]h:[m]\to[n] satisfying both identities:

g(h(x))=xfor every 1≤x≤m,g(h(x))=x\quad\text{for every }1\le x\le m, h(g(x))=f(x)for every 1≤x≤n.h(g(x))=f(x)\quad\text{for every }1\le x\le n.

If no such m,g,hm,g,h exist, report that there is no solution. Otherwise, output any valid choice. You do not need to minimize or maximize mm.

Input

The first line contains nn. The second line contains f(1),f(2),…,f(n)f(1),f(2),\ldots,f(n).

Output

Print -1 if impossible. Otherwise print mm on line one, values g(1),…,g(n)g(1),\ldots,g(n) on line two, and values h(1),…,h(m)h(1),\ldots,h(m) on line three. Require 1≤m≤1061\le m\le10^6, 1≤g(i)≤m1\le g(i)\le m, 1≤h(j)≤n1\le h(j)\le n.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤f(i)≤n1\le f(i)\le n.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; 1≤f(i)≤n1\le f(i)\le n.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤f(i)≤n1\le f(i)\le n.

Examples

Input

3
2 2 2

Output

1
1 1 1
2

Explanation

Choose m=1m=1, g(1)=g(2)=g(3)=1g(1)=g(2)=g(3)=1 and h(1)=2h(1)=2. Then g(h(1))=1g(h(1))=1 and h(g(i))=2=f(i)h(g(i))=2=f(i) for all ii.