Problem:
Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::io::Read;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    let s: Vec<char> = iterator.next().unwrap().chars().collect();

    let mut t: Vec<char> = Vec::new();
    let mut indexes: Vec<usize> = Vec::new();
    for i in 0..s.len() {
        if s[i] != 'x' {
            t.push(s[i]);
            indexes.push(i);
        }
    }

    if !is_kaibun(&t) {
        return "-1".to_string();
    }

    let (l, r) = if indexes.len() % 2 == 0 {
        (indexes[indexes.len() / 2 - 1], indexes[indexes.len() / 2])
    } else {
        (indexes[indexes.len() / 2], indexes[indexes.len() / 2])
    };
    // println!("l = {}, r = {}", l, r);

    // 中心から左方向に見ていく
    let mut counts_l: Vec<isize> = Vec::new();
    let mut c = 0;
    for i in (0..l).rev() {
        // println!("{}", s[i]);
        if s[i] == 'x' {
            c += 1;
        } else {
            counts_l.push(c);
            c = 0;
        }
    }
    counts_l.push(c);
    // println!("{:?}", counts_l);

    // 中心から右方向に見ていく
    let mut counts_r: Vec<isize> = Vec::new();
    let mut c = 0;
    for i in (r + 1)..s.len() {
        // println!("{}", s[i]);
        if s[i] == 'x' {
            c += 1;
        } else {
            counts_r.push(c);
            c = 0;
        }
    }
    counts_r.push(c);

    // println!("{:?}", counts_r);

    let mut ans = 0;
    for (l, r) in counts_l.iter().zip(counts_r.iter()) {
        ans += (*l - *r).abs();
    }
    return ans.to_string();
}

fn is_kaibun(t: &Vec<char>) -> bool {
    for i in 0..(t.len() / 2) {
        if t[i] != t[t.len() - 1 - i] {
            return false;
        }
    }
    return true;
}