Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:

  * the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are ck cards with number k on them in the subsequence, than for all pairs of integers <image> the condition |ci - cj| ≤ 1 must hold. 
  * if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition. 



Please help Vladik to find the length of the longest subsequence that satisfies both conditions.

Input

The first line contains single integer n (1 ≤ n ≤ 1000) — the number of cards in Vladik's sequence.

The second line contains the sequence of n positive integers not exceeding 8 — the description of Vladik's sequence.

Output

Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.

Examples

Input

3
1 1 1


Output

1

Input

8
8 7 6 5 4 3 2 1


Output

8

Input

24
1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8


Output

17

Note

In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
import java.io.*;
import java.util.*;
public class Main
{
	final int inf=(int)(1e9+1);
	int n,bigmask,dp[][][],occ[][],count[][];
	private void solve()throws IOException
	{
		n=nextInt();
		//occ[i][j] is the smallest index L such that j occurs i times in [1..L]
		occ=new int[n+1][9];
		//count[i][j] is the number of times j appears in [1..i]
		count=new int[n+1][9];
		
		for(int i=1;i<=n;i++)
		{
			int x=nextInt();
			for(int j=1;j<=8;j++)
				count[i][j]=count[i-1][j]+(x==j?1:0);
			occ[count[i-1][x]+1][x]=i;
		}


		bigmask=(1<<8)-1;
		//dp[mask][more][cnt] is the min index R such that a subseqeuence of a[1..R]
		// has continous segments of all set bits of mask appearing cnt or cnt+1 times
		// and more of those are appearing cnt+1 times
		dp=new int[bigmask+1][9][n+1];

		for(int mask=0;mask<=bigmask;mask++)
			for(int more=0;more<=8;more++)
				for(int cnt=0;cnt<=n;cnt++)
				{
					if(mask==0 && more==0 )
					{
						dp[mask][more][cnt]=0;
						continue;
					}
					dp[mask][more][cnt]=inf;
					for(int bit=0;bit<8;bit++)
						if(((mask>>bit)&1)==1)
						{
							int prevmask=mask^(1<<bit);
							dp[mask][more][cnt]=Math.min(dp[mask][more][cnt],search(dp[prevmask][more][cnt]+1,cnt,bit+1));
							if(more>0)
								dp[mask][more][cnt]=Math.min(dp[mask][more][cnt],search(dp[prevmask][more-1][cnt]+1,cnt+1,bit+1));
						}
				}
		int ans=0;
		for(int more=0;more<=8;more++)
			for(int cnt=0;cnt<=n;cnt++)
				if(dp[bigmask][more][cnt]!=inf)
					ans=Math.max(ans,more+cnt*8);
		out.println(ans);
	}
	//returns the smallest index r, such that x occurs cnt times in a[l..r], inf if not possible
	int search(int l,int cnt,int x)
	{
		if(l>n)
			return inf;
		if(cnt==0)
			return l-1;
		int prev=count[l-1][x];
		int req=prev+cnt;
		if(req>n)
			return inf;
		return occ[req][x]==0?inf:occ[req][x];
	}

	 
	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();
		
		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}