

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 *
 * @author muran4
 */
public class Gallery {
    
    private static class Pair {
        int x, y;

        private Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            int hash = 3;
            hash = 7900 * hash + this.x;
            hash = 79 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Pair other = (Pair) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }
        
        
        
    }
    
    public static int[] arr;
    public static HashMap<Pair, Integer> map = new HashMap<>();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String line;
        int n;     
        String[] vals;        
        while((line = input.readLine())!=null){
            map.clear();
            n = Integer.parseInt(line);
            line = input.readLine();
            vals = line.split(" ");
            arr = new int[n];
            for (int i = 0; i < vals.length; i++) {
                arr[i] = Integer.parseInt(vals[i]);
            }
            
            int pocet =  spracuj(0, n-1);
            System.out.println(pocet);
            
        }
    }

    private static int spracuj(int x, int y) {
        Pair pair = new Pair(x,y);
        //zisti existuje
        Integer count = map.get(pair);
        if(count !=null)return count;
        //
        if(x>=y)return 0;
        if(arr[x] == arr[y]){
            
            int value = 1+spracuj(x+1, y-1);
            map.put(pair, value);
            return value;
        }else{
            int xx = spracuj(x+1,y);
            int yy = spracuj(x, y-1);
            int max = Math.max(xx,yy);
             map.put(pair, max);
              return max;
        }
        
        
    }
    
}
