You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.

Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.

For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.

The game "flood fill" is played on the given line as follows:   At the start of the game you pick any starting square (this is not counted as a turn).  Then, in each game turn, change the color of the connected component containing the starting square to any other color. 

Find the minimum number of turns needed for the entire line to be changed into a single color.


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.

The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.


-----Output-----

Print a single integer — the minimum number of the turns needed.


-----Examples-----
Input
4
5 2 2 1

Output
2

Input
8
4 5 2 2 1 3 5 5

Output
4

Input
1
4

Output
0



-----Note-----

In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows:  $[5, 2, 2, 1]$  $[5, 5, 5, 1]$  $[1, 1, 1, 1]$ 

In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.

In the third example, the line already consists of one color only.

Difficulty: interview

Starter code:

Solutions:
n=int(input())
C=[0]+list(map(int,input().split()))

#n=5000
#C=list(range(n+1))

A=[]
for i in range(1,n+1):
    if C[i]!=C[i-1]:
        A.append(C[i])

L=len(A)
DP=[[[0]*L for i in range(L)] for j in range(2)]
#左の色に揃える or 右の色に揃える,左からi～j番目を

def color(r,i,j):#i<j
    if r==1:
        if A[i]==A[j]:
            DP[r][i][j]=min(DP[0][i][j-1],DP[1][i][j-1]+1)
        else:
            DP[r][i][j]=min(DP[0][i][j-1]+1,DP[1][i][j-1]+1)

    else:
        if A[i]==A[j]:
            DP[r][i][j]=min(DP[1][i+1][j],DP[0][i+1][j]+1)
        else:
            DP[r][i][j]=min(DP[1][i+1][j]+1,DP[0][i+1][j]+1)

for i in range(1,L):
    for j in range(L-i):
        color(0,j,i+j)
        color(1,j,i+j)

    #print(DP)

print(min(DP[0][0][L-1],DP[1][0][L-1]))


from itertools import groupby
import math

n, = list(map(int, input().strip().split()))
array = list(map(int, input().strip().split()))

g_array = [k for k, g in groupby(array)]

n = len(g_array)
extend_n = n + 2
sols = [[0]*extend_n for _ in range(extend_n)]

for i in range(1, extend_n-1):
	for j in range(extend_n-2, i, -1):
		val = max(sols[i-1][j], sols[i][j+1])
		if g_array[i-1] == g_array[j-1]:
			val = max(val, sols[i-1][j+1] + 1)
		sols[i][j] = val

best = 0
for start in range(1, extend_n-1-1):
	start_val = sols[start-1][start+1]
	if start_val > best:
		best = start_val

print(len(g_array) - 1 - best)



n = int(input())
a = list(map(int, input().split()))
b = []
p = -1
for e in a:
    if e == p:
        continue
    else:
        b.append(e)
        p = e

def lcs(a, b):
    lengths = [[0 for j in range(len(b)+1)] for i in range(len(a)+1)]
    # row 0 and column 0 are initialized to 0 already
    for i, x in enumerate(a):
        for j, y in enumerate(b):
            if x == y:
                lengths[i+1][j+1] = lengths[i][j] + 1
            else:
                lengths[i+1][j+1] = max(lengths[i+1][j], lengths[i][j+1])
    # read the substring out from the matrix
    x, y = len(a), len(b)
    return lengths[x][y]

ans = 0

#print(b)
#for i in range(len(b)//2+1):
    #print(list(reversed(b[:i])), b[i:])
    #ans = max(ans, lcs(list(reversed(b[:i])), b[i:]))

ans = lcs(list(reversed(b)), b)//2
n = len(b)
#print(ans)
print(n - ans - 1)

def inpl(): return list(map(int, input().split()))

N = int(input())
A = inpl()
B = [0]
for a in A:
    if B[-1] != a:
        B.append(a)

B = B[1:]
k = len(B)
dp = [0]*((k*k + k)//2)
for d in range(1, k):
    for i in range(k-d):
        if B[i] == B[i+d]:
            dp[d*(2*k-d+1)//2+i] = dp[(d-2)*(2*k-d+3)//2+i+1] + 1
        else:
            dp[d*(2*k-d+1)//2+i] = 1 + min(dp[(d-1)*(2*k-d+2)//2+i], dp[(d-1)*(2*k-d+2)//2+i+1])
print(dp[-1]) 


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

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
cN = LIST()
cN2 = [cN[0]]
for i in range(1, N):
    if cN[i] != cN[i-1]:
        cN2.append(cN[i])
cN3 = list(reversed(cN2))
N2 = len(cN2)

dp = [[0] * (N2+1) for i in range(N2+1)]

for i in range(N2+1):
    for j in range(N2+1):
        if i < N2:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j < N2:
            dp[i][j+1] = max(dp[i][j+1], dp[i][j])
        if i < N2 and j < N2 and cN2[i] == cN3[j]:
            dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1)
print(N2-1 - dp[N2][N2] // 2)


def fill():
    nonlocal dp
    dp = [0] * (n + 2)
    for i in range(n + 2):
        dp[i] = [0] * (n + 2)

def compress(v):
    nonlocal n
    nonlocal c
    c = [v[0]]
    for i in range(1, n):
        if v[i] != v[i - 1]:
            c.append(v[i])
    n = len(c)
            
n = int(input())
v = [int(i) for i in input().split()]
c = []
compress(v)
dp = []
fill()
for i in range(n):
    dp[i][1] = 0
    dp[i][2] = 1

for sz in range(3, n + 1):
    for i in range(n):
        j = i + sz - 1
        if j >= n:
            break
        if c[i] == c[j]:
            dp[i][sz] = dp[i + 1][sz - 2] + 1
        else:
            dp[i][sz] = min(dp[i + 1][sz - 1], dp[i][sz - 1]) + 1

print(dp[0][n])

n=int(input())
lsr = list(map(int, input().split()))
ls = [lsr[0]]
for le in lsr[1:]:
    if le!=ls[-1]:
        ls.append(le)

n=len(ls)
dpa=[]
dpb=[0 for i in range(n+1)]
for sz in range(2,n+1):
    dp=[]
    for start in range(n-sz+1):
        if ls[start] == ls[start+sz-1]:
            dp.append(min(dpb[start], dpb[start+1], dpa[start+1])+1)
        else:
            dp.append(min(dpb[start], dpb[start+1])+1)
    dpa, dpb = dpb, dp
print( dpb[0])

def lps(str): 
    n = len(str) 
  
    L = [[0 for x in range(n)] for x in range(n)] 
  
    for i in range(n): 
        L[i][i] = 1
  
    for cl in range(2, n+1): 
        for i in range(n-cl+1): 
            j = i+cl-1
            if str[i] == str[j] and cl == 2: 
                L[i][j] = 2
            elif str[i] == str[j]: 
                L[i][j] = L[i+1][j-1] + 2
            else: 
                L[i][j] = max(L[i][j-1], L[i+1][j]); 
  
    return L[0][n-1] 
  
n = input()
n = int(n)
l = list(map(str, input().strip().split()))
seq = [l[0]]
ch = l[0]
for i in range(1,n):
    if l[i] != ch:
        ch = l[i]
        seq.append(l[i])
n = len(seq)
print(n - lps(seq)//2 -1)

a=int(input())
b = [int(x) for x in input().split()]
c = [b[0]]
for i in b[1:]:
    if i!=c[-1]:
        c.append(i)
a=len(c)

dpa = []
dpb = [0] * (a+1)
for i in range(2,a+1):
    dp=[]
    for j in range(a-i+1):
        if c[j] == c[j+i-1]:
            dp.append(min(dpb[j], dpb[j+1], dpa[j+1])+1)
        else:
            dp.append(min(dpb[j], dpb[j+1])+1)
    dpa, dpb = dpb, dp
print(dpb[0])


def lcs(a, b):
    c = [[0 for i in range(len(a)+1)] for i in range(len(b)+1)]
    for i in range((len(b)+1)):
        for j in range(len(b)+1):
            if i == 0 or j == 0:
                c[i][j] = 0
            elif a[i-1] == b[j-1]:
                c[i][j] = c[i-1][j-1] + 1
            else:
                c[i][j] = max(c[i-1][j], c[i][j-1])
    return c


n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(1, n):
    if a[i] != a[i-1]:
        b.append(a[i])
a = b[:: -1]
c = lcs(a, b)
ans = 0
n = len(a)
for i in range(0, n+1):
    ans = max(ans, c[i][n-i])
print(n-ans-1)


n = int(input())
C = [int(i) for i in input().split()]

INF = 5000
DP = [[[0] * (2) for i in range(n)] for j in range(2)] #[width][left][L/R]

for i in range(n-1):
    for j in range(n-i-1):    
        DP[(i+1)%2][j][0] = INF
        DP[(i+1)%2][j][1] = INF
        for k in range(2):
            c = C[j] if k == 0 else C[j+i]
            if c == C[j+i+1]:
                DP[(i+1)%2][j][1] = min(DP[(i+1)%2][j][1], DP[i%2][j][k])
            else:
                DP[(i+1)%2][j][1] = min(DP[(i+1)%2][j][1], DP[i%2][j][k] + 1)
                
            c = C[j+1] if k == 0 else C[j+i+1]
            if c == C[j]:
                DP[(i+1)%2][j][0] = min(DP[(i+1)%2][j][0], DP[i%2][j+1][k])
            else:
                DP[(i+1)%2][j][0] = min(DP[(i+1)%2][j][0], DP[i%2][j+1][k] + 1)

print(min(DP[(n-1)%2][0]))

n = int(input())
C = [int(i) for i in input().split()]

D = []
for c in C:
    if not D or D[-1] != c:
        D.append(c)

n = len(D)
C = D[::-1]
DP = [[0] * (n+1) for i in range(n+1)] 

for i in range(n):
    for j in range(n):
        if C[i] == D[j]:
            DP[i+1][j+1] = DP[i][j] + 1
        else:
            DP[i+1][j+1] = max(DP[i+1][j], DP[i][j+1])

print(n - 1 - DP[n][n] // 2)

n = int(input())
C = [int(i) for i in input().split()]

D = []
for c in C:
    if not D or D[-1] != c:
        D.append(c)

n = len(D)
C = D[::-1]
DP = [[0] * (n+1) for i in range(2)] 

for i in range(n):
    for j in range(n):
        if C[i] == D[j]:
            DP[(i+1)&1][j+1] = DP[i&1][j] + 1
        else:
            DP[(i+1)&1][j+1] = max(DP[(i+1)&1][j], DP[i&1][j+1])

print(n - 1 - DP[n&1][n] // 2)

n = int(input())
raw_list = [int(item) for item in input().split()]

l = [raw_list[0]]
for item in raw_list[1:]:
    if item != l[-1]:
        l.append(item)
n = len(l)

dp1 = []
dp2 = [0] * (n + 1)
for i in range(2, n + 1):
    dp = []
    for j in range(n - i + 1):
        if l[j] == l[i + j - 1]:
            dp.append(min(dp1[j + 1], dp2[j], dp2[j + 1]) + 1)
        else:
            dp.append(min(dp2[j], dp2[j + 1]) + 1)
    dp1, dp2 = dp2, dp
res = dp2[0]
print(res)


def run_length_compress(string):
    string = string + ["."]
    n = len(string)

    begin = 0
    end = 1
    cnt = 1
    ans = [] 
    while True: 
        if end >= n:
            break
        if string[begin] == string[end]:
            end += 1
            cnt += 1
        else:
            ans.append(string[begin])
            begin = end 
            end = begin + 1
            cnt = 1

    return ans


def lps(string): 
    n = len(string)
    dp = [[0]*n for _ in range(n)] 

    for i in range(n): 
        dp[i][i] = 1
  
    for cl in range(2, n+1): 
        for i in range(n-cl+1): 
            j = i + cl - 1
            
            if string[i] == string[j] and cl == 2: 
                dp[i][j] = 2
            elif string[i] == string[j]: 
                dp[i][j] = dp[i+1][j-1] + 2
            else: 
                dp[i][j] = max(dp[i][j-1], dp[i+1][j])
  
    return dp[0][n-1]

n = int(input())
a = list(map(int, input().split()))

a = run_length_compress(a)
len_pal = lps(a)
print(len(a) - 1 - len_pal//2)


a=int(input())
b = [int(x) for x in input().split()]
c = [b[0]]
for i in b[1:]:
    if i!=c[-1]:
        c.append(i)
a=len(c)
 
dpa = []
dpb = [0] * (a+1)
for i in range(2,a+1):
    dp=[]
    for j in range(a-i+1):
        if c[j] == c[j+i-1]:
            dp.append(min(dpb[j], dpb[j+1], dpa[j+1])+1)
        else:
            dp.append(min(dpb[j], dpb[j+1])+1)
    dpa, dpb = dpb, dp
print(dpb[0])