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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
int x[100005],y[100005];
char s[100005],n[100005],bu[100005];
int cnt1=0,cnt2=0;
int main()
{
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    scanf("%s",s+1);
    int len=strlen(s+1);
    int i,j;
    for(i=len;i>=1;i--)
    {
       n[len-i+1]=s[i];
    } // printf("%s",n+1);
    int d=1;
    for(i=1;i<=len;i++)
    {
        if(s[i]!='x') {bu[d++]=s[i];cnt1++;}
        else cnt2++;
    }
    //printf("%d %d",cnt1,cnt2);
    for(i=1,j=d-1;i<d;i++,j--)
    {
        if(bu[i]!=bu[j])
        {
           printf("-1\n");return 0;
        }
    }
    int k=0,xx=0,pos,cnt=0;;
    if(cnt1%2==1)
    {
    for(i=1;i<=len;i++)
    {
       if(s[i]!='x') k++;
       else xx++;
       if(k==cnt1/2+1){ pos=i;}
    }
    for(i=1,j=len;i<=j;)
    {
        if(s[i]==s[j]){i++;j--;}
        else
        {
            if(s[i]=='x') i++;
            else if(s[j]=='x')j--;
             cnt++;
        }
    }
    }
    else
    {
    for(i=1;i<=len;i++)
    {
       if(s[i]!='x') k++;
       else xx++;
       if(k==cnt1/2){ pos=i;}
    }
    for(i=1,j=len;i<=j;)
    {
        if(s[i]==s[j]){i++;j--;}
        else
        {
            if(s[i]=='x') i++;
            else if(s[j]=='x')j--;
             cnt++;
        }
    }
    } printf("%d\n",cnt);
}