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

#GD0000157. Ông già Noel bí mật (Secret Santa)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ông già Noel bí mật (Secret Santa)

Secret Santa

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn employees numbered from 11 to nn. Each employee ii wishes to give a gift to employee ai≠ia_i\ne i. Assign a distinct recipient bib_i to each employee, with bi≠ib_i\ne i, maximizing the number of fulfilled wishes bi=aib_i=a_i.

Input

The first line contains nn. The second line contains nn integers a1,…,ana_1,\ldots,a_n, where aia_i is employee ii's wished recipient.

Output

Print the maximum number kk of fulfilled wishes and, on the next line, a permutation b1,…,bnb_1,\ldots,b_n with bi≠ib_i\ne i and exactly kk equalities bi=aib_i=a_i.

Subtasks

  • Subtask 1 (20 points): 2≤n≤82\le n\le 8; 1≤ai≤n1\le a_i\le n; ai≠ia_i\ne i.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000; 1≤ai≤n1\le a_i\le n; ai≠ia_i\ne i.
  • Subtask 3 (50 points): 2≤n≤2⋅1052\le n\le 2\cdot10^5; 1≤ai≤n1\le a_i\le n; ai≠ia_i\ne i.

Examples

Input

3
2 1 2

Output

2
3 1 2

Explanation

Employees 11 and 33 both request recipient 22, so all three wishes cannot be fulfilled. The assignment [3,1,2][3,1,2] is a derangement and fulfills the wishes of employees 22 and 33, reaching the upper bound k=2k=2.