Milk Festival

Given a sequence of shops selling milk types 0, 1, 2, find the longest subsequence whose values cycle 0,1,2,0,1,2,... in order.

Medium4Dynamic programmingNo attempts yetTime limit1sMemory limit256 MB

Problem

Yeonghak likes strawberry milk, chocolate milk, and banana milk. He is a picky drinker, so he fixed the order in which he drinks them.

  1. The very first pack he drinks is strawberry milk.
  2. After a pack of strawberry milk he drinks a pack of chocolate milk.
  3. After a pack of chocolate milk he drinks a pack of banana milk.
  4. After a pack of banana milk he drinks a pack of strawberry milk again.

The milk festival is held on Milk Street, where the milk shops stand in a single row. Each shop sells only one of the three kinds.

Yeonghak walks from the start of the street to its end in one direction and buys milk along the way. In front of each shop he either buys one pack and drinks it, or passes the shop by. The street is crowded, so he can never go back to a shop he has already passed.

Find the largest number of packs Yeonghak can drink.

Input

The first line contains the number of milk shops NN. (1N10001 \le N \le 1000)

The second line contains NN integers describing the shops in order from the start of the street to its end. 0 is a shop that sells only strawberry milk, 1 is a shop that sells only chocolate milk, and 2 is a shop that sells only banana milk. No integer other than 0, 1, or 2 is given.

Output

Print the largest number of packs Yeonghak can drink.