The 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:

  1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 
  2. For each station i there exists exactly one station j such that pj = 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 pi 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 p1, p2, ..., pn (1 ≤ pi ≤ 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 p2 to 3 and p3 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 p2 to 4 and p3 to 5.
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;


public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
//        InputStream inputStream = new FileInputStream("sum.in");
        OutputStream outputStream = System.out;
//        OutputStream outputStream = new FileOutputStream("sum.out");

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Answer solver = new Answer();
        solver.solve(in, out);
        out.close();
    }
}

class Answer {
    private final int INF = (int) (1e9 + 7);
    private final int MOD = (int) (1e9 + 7);
    private final int MOD1 = (int) (1e6 + 3);
    private final long INF_LONG = (long) (1e18 + 1);
    private final double EPS = 1e-9;

    private long gcd(long a, long b) {
        return (b == 0) ? a : gcd(b, a % b);
    }

    private List<Integer>[] G;

    private boolean[] used;
    private Map<Integer, Integer> counter = new HashMap<>();
    private int[] colors;

    private void dfs(int v, int color) {
        used[v] = true;
        colors[v] = color;

        int count = counter.getOrDefault(color, 0);
        counter.put(color, count + 1);

        for (Integer u : G[v]) {
            if (!used[u]) {
                dfs(u, color);
            }
        }
    }

    public void solve(InputReader in, PrintWriter out) throws IOException {
        int n = in.nextInt();

        G = new List[n + 1];
        for (int i = 0; i < G.length; i++) {
            G[i] = new ArrayList<>();
        }

        for (int i = 1; i <= n; i++) {
            int p = in.nextInt();
            if (i != p) {
                G[i].add(p);
            }
        }

        used = new boolean[n + 1];
        colors = new int[n + 1];

        int color = 0;

        for (int i = 1; i <= n; i++) {
            if (!used[i]) {
                dfs(i, color);
                color++;
            }
        }

        List<Integer> kols = counter.values().stream()
                .sorted().collect(Collectors.toList());

        int t = kols.size();
        if (t >= 2) {
            int k1 = kols.get(t - 1);
            int k2 = kols.get(t - 2);
            kols.set(t - 2, k1 + k2);
            t--;
        }

        long ans = 0;
        for (int i = 0; i < t; i++) {
            ans += 1L * kols.get(i) * kols.get(i);
        }

        out.println(ans);

    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int[] nextArrayInt(int count) {
        int[] a = new int[count];
        for (int i = 0; i < count; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    public long[] nextArrayLong(int count) {
        long[] a = new long[count];
        for (int i = 0; i < count; i++) {
            a[i] = nextLong();
        }
        return a;
    }
}