The company where Arthur works organizes a secret Santa every year, and unfortunately, he is responsible for organizing the game this year. In this game, every person has to send a gift to someone previously chosen at random, and on the day of the gifts exchange, when person
Since Arthur is a very distracted boy, when he randomly chose for whom each person will send a gift, he forgot that everybody must receive a gift. He also forgot to make sure that the first person to send a gift must be the last one to receive somebody's gift, due to the company's tradition. Now he has to reorganize the game. However, since his coworkers got mad with Arthur's mindlessness, they will charge a fee for Arthur to change the person they must give the gift to. Help Arthur by calculating the smallest total fee (sum of all fees) he must pay to fix the game and not get fired. For a better understanding, let's analyze the following scenario:
In the picture above, the people at the company are represented with numbers from
- Person
now gives a gift to person , and Arthur pays a fee of for that; - Person
now gives a gift to person , and Arthur pays a fee of for that; - Person
now gives a gift to person , and Arthur pays a fee of for that.
This way, Arthur pays a fee of
Input
The first line of input contains an integer
Output
Print the least total fee Arthur has to pay to fix the game.
Constraints
Subtask 1 (10 points)
In Arthur's initial choices, everyone will receive a gift from someone.
Subtask 2 (20 points)
Subtask 3 (20 points)
In Arthur's initial choices, there is only one person
Subtask 4 (30 points)
Every fee is equal to
Subtask 5 (20 points)
No more constraints.
Sample Input 1
9
2 11
3 20
1 20
3 10
2 10
5 4
4 1
9 2
8 3
Sample Output 1
23
Sample Input 2
3
1 4
1 3
1 3
Sample Output 2
7
Sample Input 3
9
3 5
4 4
5 6
2 1
1 2
2 2
4 4
5 5
3 4
Sample Output 3
15
Comments