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.util.*;
 
import java.io.*;
public class Input {
	
	
	static int binary(int []ar,int start,int end,int value){
		int i=start;
		int j=end;
		while(i<j){
			int mid=(i+j)/2;
			if(ar[mid]==value)
				return mid;
			else if(ar[mid]>value)
				j=mid;
			else
				i=mid+1;
		}
		if(j==end&&ar[j]<value)
			return j+1;
		else
			return j;
	}
	
 
	static int binary1(int []ar,int start,int end,int value){
		int i=start;
		int j=end;
		while(i<j){
			int mid=(i+j)/2;
			if(ar[mid]==value)
				return mid;
			else if(ar[mid]>value)
				j=mid;
			else
				i=mid+1;
		}
		if(j==end&&ar[j]<value)
			return j;
		else
			return j-1;
	}
	
	    public static void main(String []args) throws IOException{
 
	        Reader s=new Reader();
	    int n=s.nextInt();
	    int ar[]=new int[n+1];
	    for(int i=1;i<=n;i++){
	    	ar[i]=s.nextInt();
	    }
	    ArrayList<Integer> list=new ArrayList<Integer>();
	    boolean status[]=new boolean[n+1];
	    for(int i=1;i<=n;i++){
	    	if(!status[i]){
	    		int xx=i;
	    		status[i]=true;
	    		int count=1;
	    		int pre=ar[i];
	    		while(pre!=i){
	    			status[pre]=true;
	    			count++;
	    			pre=ar[pre];
	    		}
	    		list.add(count);
	    	}
	    }
	    Collections.sort(list);
	    if(list.size()>=2){
	    	int yy=list.get(list.size()-1)+list.get(list.size()-2);
	    	long ans=1l*yy*yy;
	    	for(int i=0;i<list.size()-2;i++){
	    		ans+=1l*list.get(i)*list.get(i);
	    	}
	    	System.out.println(ans);
	    	
	    }
	    else{
	    	long ans=0;
	    	for(int i=0;i<list.size();i++){
	    		ans+=1l*list.get(i)*list.get(i);
	    	}
	    	System.out.println(ans);
	    }
	    
	    
	    
	    }
	    static class Reader
	    {
	        final private int BUFFER_SIZE = 1 << 16;
	        private DataInputStream din;
	        private byte[] buffer;
	        private int bufferPointer, bytesRead;
	 
	        public Reader()
	        {
	            din = new DataInputStream(System.in);
	            buffer = new byte[BUFFER_SIZE];
	            bufferPointer = bytesRead = 0;
	        }
	 
	        public Reader(String file_name) throws IOException
	        {
	            din = new DataInputStream(new FileInputStream(file_name));
	            buffer = new byte[BUFFER_SIZE];
	            bufferPointer = bytesRead = 0;
	        }
	 
	        public String readLine() throws IOException
	        {
	            byte[] buf = new byte[64]; // line length
	            int cnt = 0, c;
	            while ((c = read()) != -1)
	            {
	                if (c == '\n')
	                    break;
	                buf[cnt++] = (byte) c;
	            }
	            return new String(buf, 0, cnt);
	        }
	 
	        public int nextInt() throws IOException
	        {
	            int ret = 0;
	            byte c = read();
	            while (c <= ' ')
	                c = read();
	            boolean neg = (c == '-');
	            if (neg)
	                c = read();
	            do
	            {
	                ret = ret * 10 + c - '0';
	            }  while ((c = read()) >= '0' && c <= '9');
	 
	            if (neg)
	                return -ret;
	            return ret;
	        }
	 
	        public long nextLong() throws IOException
	        {
	            long ret = 0;
	            byte c = read();
	            while (c <= ' ')
	                c = read();
	            boolean neg = (c == '-');
	            if (neg)
	                c = read();
	            do {
	                ret = ret * 10 + c - '0';
	            }
	            while ((c = read()) >= '0' && c <= '9');
	            if (neg)
	                return -ret;
	            return ret;
	        }
	 
	        public double nextDouble() throws IOException
	        {
	            double ret = 0, div = 1;
	            byte c = read();
	            while (c <= ' ')
	                c = read();
	            boolean neg = (c == '-');
	            if (neg)
	                c = read();
	 
	            do {
	                ret = ret * 10 + c - '0';
	            }
	            while ((c = read()) >= '0' && c <= '9');
	 
	            if (c == '.')
	            {
	                while ((c = read()) >= '0' && c <= '9')
	                {
	                    ret += (c - '0') / (div *= 10);
	                }
	            }
	 
	            if (neg)
	                return -ret;
	            return ret;
	        }
	 
	        private void fillBuffer() throws IOException
	        {
	            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
	            if (bytesRead == -1)
	                buffer[0] = -1;
	        }
	 
	        private byte read() throws IOException
	        {
	            if (bufferPointer == bytesRead)
	                fillBuffer();
	            return buffer[bufferPointer++];
	        }
	 
	        public void close() throws IOException
	        {
	            if (din == null)
	                return;
	            din.close();
	        }
	    }
	 
}     