Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color c_{i}. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.


-----Input-----

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is c_{i} (1 ≤ c_{i} ≤ n) — the color of the i-th gemstone in a line.


-----Output-----

Print a single integer — the minimum number of seconds needed to destroy the entire line.


-----Examples-----
Input
3
1 2 1

Output
1

Input
3
1 2 3

Output
3

Input
7
1 4 4 2 3 2 1

Output
2



-----Note-----

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

Difficulty: competition

Starter code:

Solutions:
n = int(input())
C = list(map(int, input().split()))

dp = [[0]*n for _ in range(n)]
for i in range(n) :
    dp[i][i] = 1

for i in range(n-2, -1, -1) :
    for j in range(i+1, n) :
        dp[i][j] = 1 + dp[i+1][j]
        if C[i] == C[i+1] : dp[i][j] = min( dp[i][j], 1 + (dp[i+2][j] if i+2 < n else 0) )
        for k in range(i+2, j) :
            if C[i] == C[k] : dp[i][j] = min( dp[i][j], dp[i+1][k-1] + dp[k+1][j] )
        if C[i] == C[j] and j-i > 1:
            dp[i][j] = min( dp[i][j], dp[i+1][j-1] )

print( dp[0][n-1] )

            
        


def main():
    n, l = int(input()), list(map(int, input().split()))
    dp = [[0] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = 1
    for i in range(n - 1, 0, -1):
        ci, row = l[i - 1], dp[i]
        for j in range(i, n):
            tmp = [1 + row[j]]
            if ci == l[i]:
                tmp.append(1 + dp[i + 1][j] if i + 1 < n else 1)
            for k in range(i + 1, j):
                if ci == l[k]:
                    tmp.append(row[k - 1] + dp[k + 1][j])
            if ci == l[j] and j > i:
                tmp.append(row[j - 1])
            dp[i - 1][j] = min(tmp)
    print(dp[0][n - 1])


def __starting_point():
    main()

__starting_point()

def main():
    n, l = int(input()), list(map(int, input().split()))
    dp = [[0] * n for _ in range(n + 1)]
    for le in range(1, n + 1):
        for lo, hi in zip(list(range(n)), list(range(le - 1, n))):
            row, c = dp[lo], l[lo]
            if le == 1:
                row[hi] = 1
            else:
                tmp = [1 + dp[lo + 1][hi]]
                if c == l[lo + 1]:
                    tmp.append(1 + dp[lo + 2][hi])
                for match in range(lo + 2, hi + 1):
                    if c == l[match]:
                        tmp.append(dp[lo + 1][match - 1] + dp[match + 1][hi])
                row[hi] = min(tmp)
    print(dp[0][n - 1])


def __starting_point():
    main()

__starting_point()

n = int(input())
dp = [[None for j in range(n)] for i in range(n)]
cl = input().split(' ')
cl = [int(color) for color in cl]

for bias in range(0, n):
    for l in range(n-bias):
        r = l + bias
        loc = float('+inf')
        if bias == 0:
            dp[l][r] = 1
        elif bias == 1:
            if cl[l] == cl[r]:
                dp[l][r] = 1
            else:
                dp[l][r] = 2
        else:
            if cl[l] == cl[r]:
                loc = dp[l+1][r-1]
            for k in range(l, r):
                loc = min(loc, dp[l][k]+dp[k+1][r])
            dp[l][r] = loc

print(dp[0][n-1])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])

n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])


dp = [[-1 for i in range(505)] for j in range(505)]
n = int(input())
A = [int(i) for i in input().split()]

def do(i, j):
    #print('At',i , j)
    if i>=j:
        dp[i][j] = 1
        return 1
    
    if dp[i][j] != -1:
        return dp[i][j]
    
    ans = len(A)
    if A[i] == A[j]:
        ans = min(ans, do(i+1, j-1))
    for x in range(i, j):
        
        left = do(i, x)
        right = do(x+1, j)
        ans = min(ans, left+right)

    dp[i][j] = ans
    return ans

if len(set(A)) == n:
    print(n)
else:
    print(do(0, n-1))


# -*- coding:utf-8 -*-

"""

created by shuangquan.huang at 1/9/20

"""

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List


def solve(N, A):
    dp = [[N for _ in range(N+1)] for _ in range(N)]
    for i in range(N):
        dp[i][0] = 0
        dp[i][1] = 1
    
    for l in range(2, N+1):
        for i in range(N-l+1):
            for j in range(1, l):
                dp[i][l] = min(dp[i][l], dp[i][j] + dp[i+j][l-j])
            
            # j = 0
            # while j < l // 2 and A[i+j] == A[i+l-j-1]:
            #     j += 1
            # if j >= l // 2:
            #     dp[i][l] = min(dp[i][l], 1)
            # elif j > 0:
            #     dp[i][l] = min(dp[i][l], 1 + dp[i+j][l-2*j])
            #     for k in range(i+j, i+l-j):
            #         dp[i][l] = min(dp[i][l], 1 + dp[i+j][k-i-j] + dp[k][l - 2 * j-1])
            
            if i + 1 < N and A[i] == A[i+l-1]:
                dp[i][l] = max(1, dp[i+1][l-2])
            else:
                dp[i][l] = min(dp[i][l], 1 + dp[i][l-1])
                if i + 1 < N:
                    dp[i][l] = min(dp[i][l], 1 + dp[i+1][l-1])
                    
            for j in range(i + 1, i + l - 2):
                if A[i] == A[j]:
                    dp[i][l] = min(dp[i][l], max(dp[i+1][j-i-1], 1) + max(dp[j+1][i+l-j-1], 1))
                if A[j] == A[i+l-1]:
                    dp[i][l] = min(dp[i][l], max(dp[i][j-i], 1) + max(dp[j+1][i + l - j - 2], 1))

    return dp[0][N]
    

N = int(input())
A = [int(x) for x in input().split()]
print(solve(N, A))

dp=[]
def calculate(i,j,colors):
    if i>j: return 0
    if dp[i][j]==-1:
        if i==j: 
            return 1
        dp[i][j] = 10000
        dp[i][j] = min(dp[i][j],1+calculate(i+1,j,colors))
        # print(i,j)
        if colors[i]==colors[i+1]:
            dp[i][j] = min(dp[i][j],1+calculate(i+2,j,colors))
        for k in range(i+2,j+1):
            if colors[k]==colors[i]:
                dp[i][j] = min(dp[i][j],calculate(i+1,k-1,colors)+calculate(k+1,j,colors))
    return dp[i][j]

def solve():
    t = int(input())
    colors = list(map(int, input().split()))
    [dp.append([-1]*len(colors)) for x in range(len(colors))]
    print (calculate(0,len(colors)-1,colors))

try:
    solve()
except Exception as e:
    print (e)

n = int(input())
c = [*map(int, input().split())]
inf = n + 1
dp = [[inf for _ in range(n)] for __ in range(n)]

def find(l, r):
    if l > r:
        return 0
    if l == r or (l == r - 1 and c[l] == c[r]):
        dp[l][r] = 1
        return 1
    if dp[l][r] != inf:
        return dp[l][r]
    m = 1 + find(l + 1, r)
    for i in range(l + 2, r + 1):
        if c[l] == c[i]:
            m = min(m, find(l + 1, i - 1) + find(i + 1, r))
    if c[l] == c[l + 1]:
        m = min(m, find(l + 2, r) + 1)
    dp[l][r] = m
    return m

mi = inf
for i in range(n):
    mi = min(find(0, i) + find(i + 1, n - 1), mi)
print(mi)

#Problem F - Zuma


n = int(input())
seg = n - 1
gemstones = [0]*501
i = 0
for j in input().split(' '):
    gemstones[i] = int(j)
    i +=1
matriz = [[0] * 501 for i in range(501)]

for k in range (1, n+1):
    ini = 0
    fim = k - 1
    while (fim < n):
        if (k == 1):
            matriz[ini][fim] = 1

        else:
            matriz[ini][fim] = matriz[ini + 1][fim] + 1

            if (gemstones[ini] == gemstones[ini + 1]):
                matriz[ini][fim] = min(matriz[ini][fim], matriz[ini + 2][fim] + 1)
            
            for l in range (ini + 2, fim + 1):
                if (gemstones[ini] == gemstones[l]):
                    matriz[ini][fim] = min(matriz[ini][fim], matriz[ini + 1][l - 1] + matriz[l + 1][fim])
        ini += 1
        fim += 1

print(matriz[0][seg])





from sys import stdin,stdout
for _ in range(1):#(stdin.readline())):
    n=int(stdin.readline())
    # n,m=list(map(int,stdin.readline().split()))
    a=list(map(int,stdin.readline().split()))
    dp=[[0 for _ in range(n)] for _ in range(n)]
    for sz in range(n):
        for i in range(n-sz):
            j=i+sz
            # print(i,j)
            if sz==0:dp[i][j]=1
            elif sz==1:dp[i][j]=1+int(a[i]!=a[j])
            else:
                v=n
                if a[i]==a[j]:v=dp[i+1][j-1]
                for k in range(i,j):
                    v=min(v,dp[i][k]+dp[k+1][j])
                dp[i][j]=v
    # print(*dp,sep='\n')
    print(dp[0][n-1])

from sys import stdin,stdout
for t in range(1):#int(stdin.readline())):
    n=int(stdin.readline())
    a=list(map(int,stdin.readline().split()))
    dp=[[0 for i in range(n)] for _ in range(n)]
    for sz in range(n):
        for i in range(n-sz):
            j=i+sz
            if sz<=1:
                dp[i][j]=1 if a[i]==a[j] else 2
            else:
                v=j-i+1
                for k in range(i,j):
                    v=min(v,dp[i][k]+dp[k+1][j])
                if a[i]==a[j]:v=min(v,dp[i+1][j-1])
                dp[i][j]=v
    print(dp[0][n-1])

n = int(input())
t = tuple(map(int, input().split()))
m = [[1] * n for i in range(n + 1)]
for d in range(2, n + 1):
    for i in range(n - d + 1):
        m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
        if t[i] == t[i + d - 1]: m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0])