PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000011. Khăn choàng của bà Capa (Grandma Capa Knits a Scarf)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khăn choàng của bà Capa (Grandma Capa Knits a Scarf)

Grandma Capa Knits a Scarf

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Grandma Capa wants to knit a scarf using a pattern written by Grandpa Sher. The pattern is a string ss of lowercase English letters. She considers a pattern beautiful only if it is a palindrome, meaning that it reads the same in both directions.

To modify the pattern, she may choose one letter from a to z and delete some of its occurrences. Deleting none or all of those occurrences is allowed, but deleting any other letter is forbidden. Find the minimum number of deletions needed to obtain a palindrome, or print -1 if that is impossible.

Input

  • The first line contains an integer nn, the length of the original pattern.
  • The second line contains a string ss of exactly nn lowercase English letters.

Output

Print one integer: the minimum number of deletions needed to obtain a palindrome, or -1 if it is impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; ∣s∣=n|s|=n; every character of ss is a lowercase English letter a–z.
  • Subtask 2 (30 points): 1≤n≤15001\le n\le 1500; ∣s∣=n|s|=n; every character of ss is a lowercase English letter a–z.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le 10^5; ∣s∣=n|s|=n; every character of ss is a lowercase English letter a–z.

Examples

Input

8
abcaacab

Output

2

Explanation

Choose letter a and delete its first and last occurrences. The result bcaacb is a palindrome. Deleting just one symbol cannot resolve the required mismatches.