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.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {

    static long mod = 1000000007L;
    static FastScanner scanner;

    static int[] groups, p;


    public static void main(String[] args) {
        scanner = new FastScanner();
        int n = scanner.nextInt();
        p = scanner.nextIntArray(n);
        for (int i = 0; i < n; i++) p[i]--;
        boolean[] hasTrainTo = new boolean[n];
        for (int i = 0; i < n; i++) hasTrainTo[p[i]] = true;
        groups = new int[n];
        Arrays.fill(groups, -1);

        int grp = 0;
        for (int i = 0; i < n; i++) if (!hasTrainTo[i]) dfs(i, grp++);
        int beforeCyclic = grp;
        for (int i = 0; i < n; i++) if (groups[i] == -1) dfs(i, grp++);

        Grp[] grps = new Grp[grp];
        for (int i = 0; i < n; i++) {
            if (grps[groups[i]] == null) {
                grps[groups[i]] = new Grp(groups[i] >= beforeCyclic, 0);
            }
            grps[groups[i]].cnt++;
        }
        long all = Stream.of(grps).mapToLong(xxx -> xxx.ways()).sum();
        Grp bestCyclic = Stream.of(grps).filter(xxx -> xxx.cyclic).max((aa, bb) -> aa.cnt - bb.cnt).orElse(null);
        Grp bestNonCyclic = Stream.of(grps).filter(xxx -> !xxx.cyclic).max((aa, bb) -> aa.cnt - bb.cnt).orElse(null);

        long max = all;
        if (bestCyclic != null) {
            for (int i = 0; i < grp; i++) {
                if (grps[i] == bestCyclic) continue;
                long current = new Grp(true, grps[i].cnt + bestCyclic.cnt).ways();
                max = Math.max(all + current - grps[i].ways() - bestCyclic.ways(), max);
            }
        }
        if (bestNonCyclic != null) {
            for (int i = 0; i < grp; i++) {
                if (grps[i] == bestNonCyclic) continue;
                long current = new Grp(true, grps[i].cnt + bestNonCyclic.cnt).ways();
                max = Math.max(all + current - grps[i].ways() - bestNonCyclic.ways(), max);
            }
        }
        System.out.println(max);
    }

    static void dfs(int current, int group) {
        if (groups[current] != -1) return;
        groups[current] = group;
        dfs(p[current], group);
    }

    static class Grp {
        boolean cyclic;
        int cnt;

        public Grp(boolean cyclic, int cnt) {
            this.cyclic = cyclic;
            this.cnt = cnt;
        }

        public long ways() {
            if (cyclic) {
                return (long)cnt * (long)cnt;
            } else {
                return ((long)cnt * (long)cnt + (long)cnt) / 2;
            }
        }
    }

    static class Pos implements Comparable<Pos> {
        int val, ind;

        public Pos(int val, int ind) {
            this.val = val;
            this.ind = ind;
        }

        @Override
        public int compareTo(Pos o) {
            return Integer.compare(val, o.val);
        }
    }

    static class Pt{
        long x, y;

        public Pt(long x, long y) {
            this.x = x;
            this.y = y;
        }
    }
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        String nextLine() {
            try {
                return br.readLine();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException();
            }
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }

        int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) res[i] = nextInt();
            return res;
        }

        long[] nextLongArray(int n) {
            long[] res = new long[n];
            for (int i = 0; i < n; i++) res[i] = nextLong();
            return res;
        }

        String[] nextStringArray(int n) {
            String[] res = new String[n];
            for (int i = 0; i < n; i++) res[i] = nextToken();
            return res;
        }
    }

    static class PrefixSums {
        long[] sums;

        public PrefixSums(long[] sums) {
            this.sums = sums;
        }

        public long sum(int fromInclusive, int toExclusive) {
            if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
            return sums[toExclusive] - sums[fromInclusive];
        }

        public static PrefixSums of(int[] ar) {
            long[] sums = new long[ar.length + 1];
            for (int i = 1; i <= ar.length; i++) {
                sums[i] = sums[i - 1] + ar[i - 1];
            }
            return new PrefixSums(sums);
        }

        public static PrefixSums of(long[] ar) {
            long[] sums = new long[ar.length + 1];
            for (int i = 1; i <= ar.length; i++) {
                sums[i] = sums[i - 1] + ar[i - 1];
            }
            return new PrefixSums(sums);
        }
    }

    static class ADUtils {
        static void sort(int[] ar) {
            Random rnd = ThreadLocalRandom.current();
            for (int i = ar.length - 1; i > 0; i--)
            {
                int index = rnd.nextInt(i + 1);
                // Simple swap
                int a = ar[index];
                ar[index] = ar[i];
                ar[i] = a;
            }
            Arrays.sort(ar);
        }

        static void sort(long[] ar) {
            Random rnd = ThreadLocalRandom.current();
            for (int i = ar.length - 1; i > 0; i--)
            {
                int index = rnd.nextInt(i + 1);
                // Simple swap
                long a = ar[index];
                ar[index] = ar[i];
                ar[i] = a;
            }
            Arrays.sort(ar);
        }
    }

    static class MathUtils {
        static long[] FIRST_PRIMES = {
                2,     3,      5,      7,     11,     13,     17,     19,     23,     29,
                31,     37,     41,     43,     47,     53,     59,     61,     67,     71,
                73,     79,     83,     89  ,   97 ,    101,    103,    107,    109,    113,
                127,    131,    137,    139,    149,    151,    157,    163,    167,    173,
                179,    181,    191,    193,    197,    199,    211,    223,    227,    229,
                233,    239,    241,    251,    257,    263,    269,    271,    277,    281,
                283,    293,    307,    311,    313,    317,    331,    337,    347,    349,
                353,    359,    367,    373,    379,    383,    389,    397,    401,    409,
                419,    421,    431,    433,    439,    443,    449,    457,    461,    463,
                467,    479,    487,    491,    499,    503,    509,    521,    523,    541,
                547,    557,    563,    569,    571,    577,    587,    593,    599,    601,
                607,    613,    617,    619,    631,    641,    643,    647,    653,    659,
                661,    673,    677,    683,    691,    701,    709,    719,    727,    733,
                739,    743,    751,    757,    761,    769,    773,    787,    797,    809,
                811,    821,    823,    827,    829,    839,    853,    857,    859,    863,
                877,    881,    883,    887,    907,    911,    919,    929,    937,    941,
                947,    953,    967,    971,    977,    983,    991,    997,   1009,   1013,
                1019,   1021,   1031,   1033,   1039,   1049,   1051};

        static long[] primes(long to) {
            long[] result = Arrays.copyOf(FIRST_PRIMES, 10000);
            int size = FIRST_PRIMES.length;
            a:
            for (long t = 1061; t <= to; t++) {
                for (int i = 0; i < size; i++) {
                    if (t % result[i] == 0) {
                        continue a;
                    }
                }
                result[size++] = t;
            }
            return Arrays.copyOf(result, size);
        }

        static long modpow(long b, long e, long m) {
            long result = 1;

            while (e > 0) {
                if ((e & 1) == 1) {
            /* multiply in this bit's contribution while using modulus to keep
             * result small */
                    result = (result * b) % m;
                }
                b = (b * b) % m;
                e >>= 1;
            }

            return result;
        }

        static long submod(long x, long y, long m) {
            return (x - y + m) % m;
        }
    }
}