AtCoder Grand Contest 054
A
Remove Substrings
AtCoder
⌀ 0
原题
←B →
翻译中…

Problem Statement

翻译中…

Given is a string $S$ of length $N$ consisting of lowercase English letters.

翻译中…

You can do the following operation on $S$ any number of times.

翻译中…
  • Choose its (non-empty) contiguous substring such that the first character and the last character are different, and delete this substring.
翻译中…

Determine whether it is possible to make $S$ empty. If it is possible, find the minimum number of operations needed to do so.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 10^5$
  • $S$ is a string of length $N$ consisting of lowercase English letters.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N
S

翻译中…

Output

翻译中…

If it is possible to make $S$ empty, print the minimum number of operations needed to do so. Otherwise, print $-1$.

翻译中…

样例

Input #1
4
abba
Output #1
2
We should do as follows: `abba` → (delete `ab`) → `ba` → (delete `ba`) → an empty string.
Input #2
3
aba
Output #2
-1
题目信息
题号
agc054_a
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。