
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
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<>();
    public static HashMap<Integer,ArrayList<Integer>> uuu = 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];
            uuu.clear();;
            max = 0;
            for (int i = 0; i < vals.length; i++) {
                arr[i] = Integer.parseInt(vals[i]);
                ArrayList list = uuu.get(arr[i]);
                if(list == null ){
                    list = new ArrayList();
                    uuu.put(arr[i], list);
                }
                list.add(i);
                
            }
            int result = spracuj(0, vals.length-1);
            System.out.println(max);
        }
    }
    
    
    public static int max;
    
     private static int spracuj(int x, int y) {
//         System.out.println(x+" "+y+" "+max);
        Pair pair = new Pair(x,y);
        //zisti existuje
        Integer count = map.get(pair);
        if(count !=null)return count;
        //
        if(x>=y)return 0;
        
        
        int val = 0;
        ArrayList<Integer> list;
        int zac;
        
        int cmax = 0;
        for (int i = x; i <= y; i++) {
            pair = new Pair(i,y);
            zac = arr[i];
            int last;
            list = uuu.get(zac);
            if(list.isEmpty())continue;
            
//            last = list.get(list.size()-1);
            last = -1;
            for (Integer integer : list) {
                if(integer <=y && integer >i)last = integer;
            }
//            System.out.print;
            if(last == -1)continue;
            if(i < last){
                val = spracuj(i+1, last-1)+1;
                
                map.put(pair, val);
                if(val > max)max = val;
                if(val > cmax)cmax = val;
            }
        }
        
      
        return cmax;
//            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;
//        }
//        
//        
//    }

   
    
}
