Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

The problem statement has recently been changed. View the changes.

×
C. Bertown Subway

time limit per test

1 secondmemory limit per test

256 megabytesinput

standard inputoutput

standard outputThe construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.

There are *n* stations in the subway. It was built according to the Bertown Transport Law:

- For each station
*i*there exists exactly one train that goes from this station. Its destination station is*p*_{i}, possibly*p*_{i}=*i*; - For each station
*i*there exists exactly one station*j*such that*p*_{j}=*i*.

The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (*x*, *y*) such that person can start at station *x* and, after taking some subway trains (possibly zero), arrive at station *y* (1 ≤ *x*, *y* ≤ *n*).

The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of *p*_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.

The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get!

Input

The first line contains one integer number *n* (1 ≤ *n* ≤ 100000) — the number of stations.

The second line contains *n* integer numbers *p*_{1}, *p*_{2}, ..., *p*_{n} (1 ≤ *p*_{i} ≤ *n*) — the current structure of the subway. All these numbers are distinct.

Output

Print one number — the maximum possible value of convenience.

Examples

Input

3

2 1 3

Output

9

Input

5

1 5 4 3 2

Output

17

Note

In the first example the mayor can change *p*_{2} to 3 and *p*_{3} to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3).

In the second example the mayor can change *p*_{2} to 4 and *p*_{3} to 5.

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Mar/06/2021 15:15:05 (h2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|