#P1053. [NOIP 2005 提高组] 篝火晚会

[NOIP 2005 提高组] 篝火晚会

Description

Jiajia has just entered high school. During military training, because Jiajia is hardworking and resilient, she quickly earned the instructor’s appreciation and became a “junior instructor” (xiao jiaoguan). On the last night of the training, Jiajia was ordered to organize a bonfire party. There are nn students, numbered from 11 to nn. Initially, the students sit in a circle in the order 1,2,,n1, 2, \cdots, n. In reality, each person has two classmates they most wish to sit next to. How should Jiajia issue commands to rearrange the students into a new circle that satisfies everyone’s wishes? This is a big challenge for Jiajia.

Jiajia can issue commands, each of the following form:

(b1,b2,...,bm1,bm)(b_1, b_2, ..., b_{m-1}, b_m)

Here, the value of mm is determined by Jiajia and can be different for each command. This command moves the positions of the mm students with numbers b1,b2,,bmb_1, b_2, \cdots, b_m: b1b_1 moves to b2b_2’s position, b2b_2 moves to b3b_3’s position, …, and bmb_m moves to b1b_1’s position. Executing each command incurs a cost. We assume that if a command moves mm people, then its cost is mm. We need Jiajia to achieve everyone’s wishes with the minimum total cost. Can you help Jiajia?

Input Format

The first line contains an integer nn, the total number of students.

The next nn lines each contain 22 distinct positive integers, separated by a space. The ii-th of these lines gives the numbers of the two classmates whom student ii most wishes to sit next to.

Output Format

Output a single integer: the minimum total cost. If it is impossible to satisfy every student’s wish no matter how you rearrange, output 1-1.

4
3 4
4 3
1 2
1 2

2

Hint

  • For 30%30\% of the testdata, n1000n \le 1000.
  • For 100%100\% of the testdata, 3n500003 \le n \le 50000.

Source: NOIP 2005 Senior, Problem 3.

Translated by ChatGPT 5