var
a:array[0..5000,0..5000]of longint;
b,c,d:array[1..5000]of integer;
n,i,j,k,min,t,x1,y1,x2,y2,l1,l2:longint;
ch:char;
procedure empty(x,y:longint);
var
i,j:longint;
begin
for i:=0 to x do
for j:=0 to y do
a[i,j]:=0;
end;
procedure doit;
var
i,j,p,q,r,max:longint;
begin
if(x1=y1)or(x2=y2)then begin
t:=n;
exit;
end;
for i:=x1 to y1 do
for j:=x2 to y2 do
if c[i]=d[j]then
begin
p:=a[i-x1,j-x2]+1;
q:=a[i-x1,j-x2+1];
r:=a[i-x1+1,j-x2];
if p>q then max:=p else max:=q;
if r>max then max:=r;
a[i-x1+1,j-x2+1]:=max;
end
else begin
q:=a[i-x1,j-x2+1];
r:=a[i-x1+1,j-x2];
if q>r then max:=q else max:=r;
a[i-x1+1,j-x2]:=max;
end;
t:=l1-a[l1,l2]+l2-a[l1,l2];
end;
begin
readln(n);
min:=maxlongint;
for i:=1 to n do
begin
read(ch);
b[i]:=ord(ch);
end;
d:=b;
for i:=n downto 1 do
c[n-i+1]:=b[i];
x1:=n;
y1:=n;
x2:=1;
y2:=n;
for i:=1 to n do
begin
if y1=x1 then l1:=0;
if x2=y2 then l2:=0;
empty(l1,l2);
doit;
dec(x1);
inc(x2);
if t<min then min:=t;
end;
writeln(min);
end.
a:array[0..5000,0..5000]of longint;
b,c,d:array[1..5000]of integer;
n,i,j,k,min,t,x1,y1,x2,y2,l1,l2:longint;
ch:char;
procedure empty(x,y:longint);
var
i,j:longint;
begin
for i:=0 to x do
for j:=0 to y do
a[i,j]:=0;
end;
procedure doit;
var
i,j,p,q,r,max:longint;
begin
if(x1=y1)or(x2=y2)then begin
t:=n;
exit;
end;
for i:=x1 to y1 do
for j:=x2 to y2 do
if c[i]=d[j]then
begin
p:=a[i-x1,j-x2]+1;
q:=a[i-x1,j-x2+1];
r:=a[i-x1+1,j-x2];
if p>q then max:=p else max:=q;
if r>max then max:=r;
a[i-x1+1,j-x2+1]:=max;
end
else begin
q:=a[i-x1,j-x2+1];
r:=a[i-x1+1,j-x2];
if q>r then max:=q else max:=r;
a[i-x1+1,j-x2]:=max;
end;
t:=l1-a[l1,l2]+l2-a[l1,l2];
end;
begin
readln(n);
min:=maxlongint;
for i:=1 to n do
begin
read(ch);
b[i]:=ord(ch);
end;
d:=b;
for i:=n downto 1 do
c[n-i+1]:=b[i];
x1:=n;
y1:=n;
x2:=1;
y2:=n;
for i:=1 to n do
begin
if y1=x1 then l1:=0;
if x2=y2 then l2:=0;
empty(l1,l2);
doit;
dec(x1);
inc(x2);
if t<min then min:=t;
end;
writeln(min);
end.