QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 10

#5212. Palindrome

統計

Bajtek, thanks to attending a computer science club, learned what a palindrome is. A palindrome is a word that reads the same from left to right as it does from right to left. For example, the words "oko", "kajak", "kobyłamamałybok", and "ababbaba" are palindromes, while the words "kajaki", "zoo", "alamakota", and "abaababa" are not.

Excited by his new knowledge, the boy quickly opened his notepad (not a notebook, but a program) and wrote down a word consisting of the letters 'a' and 'b'. After a moment of reflection, however, it dawned on him that his word might not necessarily be a palindrome. He decided to fix that! In one second, the boy can choose two adjacent letters and swap them. Will he be able to make his word a palindrome by performing a sequence of such moves (or by doing nothing)? If so, what is the minimum number of seconds such changes will take? Help him and write a program that calculates this!

Input

The single line of input contains a non-empty word written in Bajtek's notepad. The word can only contain the characters 'a' and 'b', and its length will not exceed 200,000 characters.

Output

The output should contain a single integer representing the minimum number of seconds needed to change the word from Bajtek's notepad into a palindrome. If it is not possible, the number -1 should be output instead.

Examples

Input 1

abbaaab

Output 1

2

Note 1

In the first example test, Bajtek can (for example) perform the sequence of changes abbaaabbabaaabbaabaab, which correctly turns his word into a palindrome, taking him two seconds. It can be shown that it is impossible to turn his word into a palindrome faster.

Input 2

ab

Output 2

-1

Note 2

In the second example test, Bajtek's word can be in the form ab or ba. Neither of these words is a palindrome, so the boy will not be able to complete the task.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.