#agc048a. [agc048_a]atcoder < S
[agc048_a]atcoder < S
Problem Statement
Given is a string consisting of lowercase English letters. Snuke can do the operation of swapping two adjacent characters in . For example, if agc
, one operation can change it to gac
(by swapping a
and g
) or acg
(by swapping g
and c
).
Snuke wants to do this operation zero or more times so that atcoder
holds lexicographically.
The definition of the lexicographic relation
For strings and , it is said that holds lexicographically if and only if one of the following conditions is satisfied:
- There exists an integer () such that the first characters of and those of are equal and the -th character of is strictly smaller than that of in alphabetical order.
- holds, and the first characters of are equal to .
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations needed.
Solve test cases in an input file.
Constraints
- is a string consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format. The first line of input is as follows:
Then, test cases follow, each of which is in the following format:
Output
For each test case, print if the objective is unachievable, and print the minimum number of operations needed if it is achievable. Use one line for each case.
Sample Input 1
3
atcodeer
codeforces
aaa
Sample Output 1
1
0
-1
- The first test case: for example, swapping the last two characters makes
atcodere
atcoder
hold. - The second test case: before doing any operation, we already have
codeforces
atcoder
. - The third test case: no sequence of operations makes
atcoder
hold.