
var a : array[0 .. 500] of longint;
    dp : array[0 .. 500, 0 .. 500] of longint;
    k, n, i, j, last, len, cur : longint;
 
begin
    assign(input, 'war.in');
    assign(output, 'war.out');
    reset(input);
    rewrite(output);
    read(n);
    for i := 1 to n do begin
        read(a[i]);
    end;
    last := -maxlongint;
    j := 0;
    for i := 1 to n do begin
        if last <> a[i] then begin
            inc(j);
            a[j] := a[i];
        end;
        last := a[i];
    end;
    n := j;
    for i := 1 to n do dp[i][i] := 0;
    for i := 1 to n - 1 do dp[i][i + 1] := 1;
    for len := 2 to n - 1 do begin
        for i := 1 to n - len do begin
            j := i + len;
            cur := maxlongint;
            if a[i] = a[j] then begin
                if cur > dp[i + 1][j - 1] + 1 then cur := dp[i + 1][j - 1] + 1;
                for k := i + 1 to j - 1 do
                    if a[i] = a[k] then
                        if cur > dp[i][k] + dp[k][j] then
                            cur := dp[i][k] + dp[k][j];
            end;
            for k := i + 1 to j do
                if cur > dp[i][k - 1] + dp[k][j] + 1 then  
                    cur := dp[i][k - 1] + dp[k][j] + 1;
            dp[i][j] := cur;
        end;
    end;
    writeln(dp[1][n] + 1);
(*    for i := 1 to n do
        for j := i to n do 
            writeln(i, ' ', j,' ', dp[i][j]);*)
end.
