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 PyPy3 (2.4.0) (judge verdict: Wrong Answer)

import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

s = STR()
t = []
for i in range(len(s)):
    if s[i] != 'x':
        t.append(s[i])
i = 0
j = len(t) - 1
flag = True
while i < j:
    if t[i] != t[j]:
        flag = False
        break
    i += 1
    j -= 1
if not flag:
    print(-1)
    exit()
medidx = 0
medidxl = 0
j = 0
for i in range(len(s)):
    if s[i] == 'x':
        continue
    if j == (len(t) - 1) // 2:
        medidxl = i
    if j == len(t) // 2:
        medidx = i
        break
    j += 1
i = 0
j = len(s) - 1
ans = 0
while medidx < j:
    if (s[i] == 'x' and s[j] == 'x') or (s[i] != 'x' and s[j] != 'x'):
        #print('OK', i, j)
        i += 1
        j -= 1
    elif s[i] == 'x':
        #print('a', i, j)
        i += 1
        ans += 1
    elif s[j] == 'x':
        #print('b', i, j)
        j -= 1
        ans += 1
ans += medidx - medidxl - 1 if medidx > medidxl else 0
#print(ans)
ans += medidxl - i
print(ans)