Problem:
 Score : 300 points    Problem Statement Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.  A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder , zscoder and agc are diverse words while gotou and connect aren't diverse words.  Given a diverse word S , determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S , or determine that it doesn't exist.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j} .      Constraints  1 \leq |S| \leq 26  S is a diverse word.         Input Input is given from Standard Input in the following format:  S       Output Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.        Sample Input 1 atcoder      Sample Output 1 atcoderb  atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder . Note that atcoderb is lexicographically smaller than b .       Sample Input 2 abc      Sample Output 2 abcd       Sample Input 3 zyxwvutsrqponmlkjihgfedcba      Sample Output 3 -1  This is the lexicographically largest diverse word, so the answer is -1 .       Sample Input 4 abcdefghijklmnopqrstuvwzyx      Sample Output 4 abcdefghijklmnopqrstuvx

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
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 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()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
if len(set(s)) == 26:
    for i in reversed(range(len(s))):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()
ans = [i for i in s]
for i in reversed(range(len(s))):
    tmp = chr(ans[i] + 1)
    if not tmp in ans:
        ans[i] = tmp
    print(''.join(ans))

Solution (Python):
from bisect import *

alpha = list(chr(c) for c in range(ord("a"), ord("z") + 1))

S = list(input())

not_used = list(set(alpha) - set(S))
not_used.sort()

if not not_used:
    can_use = []
    flag = False
    for i in range(len(S) - 1, -1, -1):
        
        idx = bisect_left(can_use, S[i])
        if idx == len(can_use): 
            can_use.append(S[i])
        else: 
            S[i] = can_use[idx]
            idx = i
            flag = True
            break
    if flag:
        print(*S[:idx + 1], sep="")
    else:
        print(-1)
else:
    S.append(not_used[0])
    print(*S, sep="")

Solution (Python):
t="abcdefghijklmnopqrstuvwxyz"
s=input()
if s==t[::-1]:print(-1);exit()
if len(s)!=26:
  for i in t:
    if i not in s:print(s+i);exit()
i=25
while s[i-1]>s[i]:i-=1
tt=s[i-1]
ss=list(s[i-1:])
ss.sort()
print(s[:i-1]+ss[ss.index(tt)+1])

Solution (Python):
alp = list("abcdefghijklmnopqrstuvwxyz")
s=input()
n=len(s)
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif n<26:
    for i in alp:
        if i not in s:
            print(s+i)
            break
else:
    for i in range(len(s)-1,-1,-1):
        for j in range(ord(s[i])+1,ord('z')+1):
            if chr(j) not in s[:i]:
                print(s[:i]+chr(j))
                exit()

Solution (Python):
s = input()
alp = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for i in alp:
        if i not in s:
            print(s+i)
            break
else:
    ans = ""
    for i in range(len(s)-1):
        f = True
        for j in range(i, len(s)-1):
            if ord(s[j]) < ord(s[j+1]):
                f = False
                break
        if f:
            t = sorted(list(s[i:]))
            for k in t:
                if ord(s[i-1]) < ord(k):
                    print(s[:i-1] + k)
                    break
            exit()
    print(s[:-2] + s[len(s)-1])

Solution (Python):
import collections
s  = list(input())
n = len(s)
alp = "abcdefghijklmnopqrstuvwxyz"
alp = list(alp)
co_alp = collections.Counter(alp)
if n==26:
    for i in range(n-1):
        if s[-i-1] >s[-i-2]:
            q = "z"
            for sj in s[-i-1:]:
                if sj >s[-i-2]:
                    q = min(q,sj)
            l = s[:-i-2]
            l.append(q)
            print("".join(l))
            break
    else:
        print(-1)
    exit()

for i in range(n):
    co_alp[s[i]]+=1
for w,num in sorted(co_alp.items()):
    if num==1:
        s.append(w)
        break
print("".join(s))

Solution (Python):
from string import ascii_lowercase


def main():
    word = input()
    if len(word) < 26:
        count = {}
        for a in ascii_lowercase:
            count[a] = 0
        for w in word:
            count[w] += 1
        count_l = list(count.items())
        count_l.sort(key=lambda x: x[0])
        add = ""
        for i in range(26):
            if count_l[i][1] == 0:
                add = count_l[i][0]
                break
        print(word + add)
    elif word == ascii_lowercase[::-1]:
        print(-1)
    else:
        is_printed = False
        while len(word) > 0 and not is_printed:
            for c in ascii_lowercase[ascii_lowercase.index(word[-1]) + 1:]:
                if c not in word[:-1]:
                    print(word[:-1] + c)
                    is_printed = True
                    break
            else:
                word = word[:-1]


if __name__ == '__main__':
    main()

Solution (Python):

import sys

def solve(S: str):
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return

    if len(S) < 26:
        res = sorted(list(set("zyxwvutsrqponmlkjihgfedcba")-set(S)))
        print(S+res[0])
    else:
        prev = None
        for i in range(25,-1,-1):
            if prev == None or prev < S[i]:
                prev = S[i]
                continue
            else:
                res = sorted(list(set("zyxwvutsrqponmlkjihgfedcba")-set(S[:i+1])))
                for s in res:
                    if s > S[i]:
                        print(S[:i]+s)
                        return
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    solve(S)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
alpha = "abcdefghijklmnopqrstuvwxyz"
invalpha = "zyxwvutsrqponmlkjihgfedcba"
D = {alpha[i]: i for i in range(26)}
E = [0 for i in range(26)]

for i in range(len(S)):
  E[D[S[i]]] = 1
if min(E) == 0:
  for m in range(26):
    if E[m] == 0:
      S = S[:] + alpha[m]
      break
  print(S)
elif S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
else:
  p = 26
  for i in range(25, 0, -1):
    if D[S[i]] < D[S[i-1]]:
      p = i
    else:
      break
  m = D[S[p-2]]
  L = []
  for x in range(p-1, 26):
    if D[S[x]] >= m + 1:
      L.append(S[x])
  L.sort()
  if L != []:
    S = S[:p-2] + L[0]
  else:
    S = S[:p-2] + 'z'
  print(S)

Solution (Python):
from bisect import bisect_left, insort_left

S = list(input())

alpha = [chr(i) for i in range(ord("a"), ord("a") + 26)]

not_used = list(set(alpha) - set(S))
not_used.sort()

if not not_used:
    can = []
    flag = True
    for i in range(len(S)-1, -1 , -1):
        idx = bisect_left(can, S[i])
        if idx == len(can):
            insort_left(can, S[i])
        else:
            S[i] = can[idx]
            idx = i
            flag = False
            break
    if flag:
        print(-1)
    else:
        print(*S[:idx+1], sep="")
else:
    S.append(not_used[0])
    print(*S, sep="")

Solution (Python):
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    quit()
elif len(s)<26:
    d=set([i for i in range(26)])
    for i in s:
        d.remove(ord(i)-97)
    print(s+chr(min(d)+97))
else:
    d=[ord(s[-1])]
    for i in range(25):
        c=s[-2-i]
        if max(d)<ord(c):
            d.append(ord(c))
        else:
            print(s[:26-2-i]+chr(min([i for i in d if i>ord(c)])))
            break

Solution (Python):
alpha = "abcdefghijklmnopqrstuvwxyz"

s = input()
if len(s) < 26:
  a = sorted(list(set(alpha) - set(s)))
  print(s+a[0])
  exit(0)

def a():
  A = [chr(i) for i in range(ord('a'), ord('z')+1)]
  B = {}
  for i in range(26):
    B[A[i]] = i
  C = [0]*26
  for i in range(len(s)):
    C[B[s[i]]] = 1
  for i in range(25, -1, -1):
    t = s[i]
    for j in range(B[s[i]]+1, 26):
      if C[j] == 0:
        return s[:i] + A[j]
    C[B[t]] = 0
  return -1

print(a())

Solution (Python):
s=input()
if len(s)<26:
    for i in range(26):
        c=chr(ord("a")+i)
        if c not in s:
            print(s+c)
            exit()
else:
    for i in reversed(range(1,26)):
        if s[i-1]<s[i]:
            s1=s[0:i-1]
            for j in range(26):
                c=chr(ord("a")+j)
                if c > s[i-1] and c not in s1:
                    print(s1+c)
                    exit()
    print(-1)

Solution (Python):
s = input()

if len(s) < 26:
    
    for i in range(26):
        candi_ch = chr(ord('a') + i)
        if candi_ch not in s:
            print(s + candi_ch)
            exit()
else:
    for i in reversed(range(1, 26)):
        if s[i-1] < s[i]:
            s1 = s[0:i-1]
            for j in range(26):
                candi_ch = chr(ord('a') + j)
                if candi_ch > s[i-1] and candi_ch not in s1:
                    print(s1 + candi_ch)
                    exit()
    
    print(-1)

Solution (Python):
import sys
def main():
    s = input().rstrip()
    nums = [ord(c)-ord("a") for c in s]
    if len(s) < 26:
        c = ""
        for x in range(26):
            f = True
            for y in nums:
                if x == y:
                    f = False
                    break
            if f:
                c = chr(x + ord("a"))
                break
        print(s+c)
    else:
        if s == "zyxwvutsrqponmlkjihgfedcba":
            print("-1")
        else:
            x = -1
            for i in reversed(range(25)):
                if nums[i] < nums[i+1]:
                    x = i
                    break
            p = 26
            for i in range(x+1, 26):
                if nums[x] < nums[i] and nums[i] < p:
                    p = nums[i]
            print(s[:x]+chr(p+ord("a")))

if __name__ == "__main__":
    main()

Solution (Python):
s = list(input())
q = set(list(s))
alpha = set([chr(i+ord('a')) for i in range(26)])
if alpha - q:
    use = alpha - q
    use = list(use)
    use.sort()
    print("".join(s) + use[0])
    exit()
for i in range(len(s)-1,-1,-1):
    asc = ord(s[i])
    q = set(s[:i+1])
    for j in range(asc,ord('z')+1):
        if chr(j) not in q:
            s[i] = chr(j)
            print("".join(s[:i+1]))
            exit()
print(-1)

Solution (Python):
S=input()
A=[chr(ord('a') + i) for i in range(26)]
if S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
else:
  if len(S)<26:
    for i in A:
      if str(i) not in S:
        print(S+str(i))
        exit()
  else:
    for i in range(24,-1,-1):
      for j in range(25,i,-1):
        if S[i] < S[j]:
          print(S[:i]+S[j])
          exit()

Solution (Python):
S=input()
alphabet=[chr(i) for i in range(ord('a'),ord('z')+1)]
if len(S)!=26:
    for x in alphabet: 
        if not(x in set(S)):
            flag=1
            break
    ans=S+x
    print(ans)
else:
    s=ord(S[25])
    j=25
    for i in range(24,-1,-1):
        if s<ord(S[i]):
            j=i
            s=ord(S[i])
        else:
            break

    if j==0:
        print(-1)
    else:
        l=S[j:]
        y=300
        for x in list(map(ord,l)):
            if ord(S[j-1])<x:
                y=min(y,x)
        ans=S[:j-1]+chr(y)
        print(ans)

Solution (Python):
import string
s=input()
n=len(s)
l=string.ascii_lowercase
if n<26:
    for c in l:
        if c not in s:
            print(s+c)
            exit()
elif s==l[::-1]:
    print(-1)
    exit()
else:
    for i in range(24,-1,-1):
        for j in range(25,i,-1):
            if s[i]<s[j]:
                print(s[:i]+s[j])
                exit()

Solution (Python):
S=input()
R="zyxwvutsrqponmlkjihgfedcba"
A=set(R)
if len(S)<=25:
    print(S+min(A-set(S)))
elif S==R:
    print(-1)
else:
    ok=set()
    while S:
        ok.add(S[-1])
        S=S[:-1]
        L=[s for s in ok if s > S[-1]]
        if L:
            print(S[:-1]+min(L))
            break

Solution (Python):
from string import ascii_lowercase
s = input()

if s == ascii_lowercase[::-1]:
    print(-1)
elif len(s)!=len(ascii_lowercase):
    s1 = set(list(s))
    s2 = set(list(ascii_lowercase))
    w = min(s2-s1)
    print(s+w)
else:
    back = ''
    tmp = ''
    pos = 0
    for i in range(len(s))[::-1]:
        if i==25:
            back += s[i]
            tmp = s[i]
        elif s[i]>tmp:
            back += s[i]
            tmp = s[i]
        else:
            pos = i
            break
    
    res = s[:pos]
    w = s[pos]
    l = []
    for b in back:
        if b > w:
            l.append(b)
    print(res + min(l))

Solution (Python):
def examA():
    S = SI()
    N = len(S)
    if N<26:
        d = defaultdict(bool)
        for s in S:
            d[s] = True
        for a in alphabet:
            if not d[a]:
                ans = S + a
                print(ans)
                return
    keep = []
    for i in range(N-1)[::-1]:
        keep.append(S[i+1])
        if S[i]<S[i+1]:
            break
    
    n = len(keep)
    c = "zz"
    for s in keep:
        if s>S[(N-n-1)]:
            c = min(c,s)
    if c=="zz":
        print(-1)
        return
    ans = S[:(N-n-1)] + c
    print(ans)
    return

def examB():
    N = I()
    ans = [2*(i+1) for i in range(N)]
    if N%2==0:
        for i in range(4998):
            ans[-3-i] = 3+i*6

    print(" ".join(map(str,ans)))
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examA()

"""

"""

Solution (Python):
S = input()
al=[chr(ord('a') + i) for i in range(26)]

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()

if len(S) != 26:
    check =[[0] * 2 for _ in range(26)]
    for i in range(len(S)):
        for a in range(26):
            if S[i] == al[a]:
                check[a][0] = 1
                check[a][1] = i
    for i in range(26):
        if check[i][0] == 0:
            used_al = al[i]
            break
    print(S + used_al)
    exit()
ans = ""
lst = []
for i in range(24, -1, -1):
    if S[i] > S[i+1]:
        lst.append(S[i+1])
    else:
        lst.append(S[i+1])
        tmp = S[i]
        lst.append(S[i])
        ans += S[:i]
        lst.sort()
        for j in range(len(lst)):
            if tmp == lst[j]:
                ans += lst[j+1]
                break
        print(ans)
        exit()

Solution (Python):
s = input()

if len(s) < 26:
    a = [0 for _ in range(26)]

    for i in range(len(s)):
        a[ord(s[i]) - ord("a")] += 1

    ans = s
    for i in range(26):
        if a[i] == 0:
            ans += chr(i+ord("a"))
            break

    print(ans)
    exit()


for i in range(24,-1,-1):
    for j in range(25,i,-1):
        if s[i]<s[j]:
            print(s[:i]+s[j])
            exit()

print(-1)

Solution (Python):
s = input()

if len(s) < 26:
    for i in range(26):
        c = chr(ord('a') + i)
        if not c in s:
            print(s + c)
            exit()
elif s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
else:
    for i in range(24,-1,-1):
        for j in range(25,i,-1):
            if s[i] < s[j]:
                print(s[:i] + s[j])
                exit()

Solution (Python):
berb = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

S = list(input())

for i in range(len(S)):
    S[i] = berb.index(S[i])

S_d = [-1]
S_0 = len(S)
k = 0

while True:
    if k == 26:
        if len(S) == 0:
            print(-1)
            break
        else:
            S_d.append(S.pop(-1))
            k = 0
    elif k in S or k <= S_d[-1]:
        k += 1
    else:
        S.append(k)
        for j in range(len(S)):
            S[j] = berb[S[j]]

        print(''.join(S))
        break

Solution (Python):
S=input()
abc=[chr(ord('a')+i) for i in range(26)]

if len(S)<26:
    for s in S:
        abc.remove(s)
    print(S+abc[0])
    exit()
for i in range(24,-1,-1):
    for j in range(25,i,-1):
        if S[i]<S[j]:
            print(S[:i]+S[j])
            exit()

print(-1)

Solution (Python):
def main():
    S = input()
    N = len(S)
    dic = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
           'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    for d in dic:
        if d in S:
            continue
        else:
            return print(S + d)
    if S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)
    pre = S[-1]
    used = [S[-1]]
    from heapq import heapify, heappop, heappush
    heapify(used)
    for i in range(N-1)[::-1]:
        if S[i] < pre:
            c = heappop(used)
            while S[i] > c:
                c = heappop(used)
            print(S[:i] + c)
            break
        else:
            pre = S[i]
            heappush(used, pre)


if __name__ == '__main__':
    main()

Solution (Python):

s=input()
n=len(s)
mozi=[0]*26
ans=''
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif n<26:
    for ss in s:
        mozi[ord(ss)-97]+=1
    for i in range(len(mozi)):
        if mozi[i]==0:
            break
    ans=s
    ans+=chr(97+i)
    print(ans)
elif n==26:
    for i in range(1,27):
        if s[-i]>s[-i-1]:
            break
    ss=sorted(s[-i:])
    for sss in ss:
        if sss>s[-i-1]:
            break
    print(s[:-i-1]+sss)

Solution (Python):
alp = 'abcdefghijklmnopqrstuvwxyz'
s = input()
if s == alp[::-1]:
    print(-1)
elif len(s) == 26:
    for i in range(len(s)-1,0,-1):
        if s[i] > s[i-1]:
            for j in alp:
                if j not in s[:i] and j > s[i-1]:
                    print(s[:i-1]+j)
                    break
            break
else:
    for i in alp:
        if i not in s:
            print(s+i)
            break

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=list(ST())
    if S==list("abcdefghijklmnopqrstuvwxyz"):
        print("abcdefghijklmnopqrstuvwxz")
        return 
    exist = defaultdict(int)
    N=len(S)
    for s in S:
        exist[s]+=1
    if len(S)==26:
        flag = False
        for i in range(N)[::-1]:
            s = chr(ord("a")+i)
            if S[i]==s:
                flag = True
                continue
            if flag:
                exist_s = set(S[:i+1])
                for j in range(26):
                    t = chr(ord("a")+j)
                    if t not in exist_s and t>S[i]:
                        S[i]=t
                        break
            else:
                done = False
                for i in range(N-1)[::-1]:
                    if S[i]<S[i+1]:
                        for j in range(N)[::-1]:
                            if S[i]<S[j]:
                                S[i],S[j] = S[j],S[i]
                                
                                S=S[:i+1]
                                done = True
                                break
                    if done:
                        break
                if done:
                    print(*S,sep="")
                else:
                    print(-1)
                return
            break
        print(*S,sep="")
    else:
        for i in range(26):
            s = chr(i+ord("a"))
            if exist[s]==0:
                S+=s
                break
        print(*S,sep="")
        
if __name__ == '__main__':
    main()

Solution (Python):
S=input()
if S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
  exit()
alp='abcdefghijklmnopqrstuvwxyz'
N=len(S)
if N<26:
  T=S+alp
  i=0
  while T.index(alp[i])<N:
    i+=1
  print(S+alp[i])
  exit()
  
i=-1
nums=[alp.index(S[i])]
while max(nums)==nums[-1]:
  i-=1
  nums.append(alp.index(S[i]))

out=S[:-len(nums)]
tmp=26
for i in nums[:-1]:
  if i>nums[-1] and tmp>i:
    tmp=i
    
print(out+alp[tmp])

Solution (Python):
S = input()

if S == "".join([chr(i) for i in reversed(range(97, 97 + 26))]):
    print(-1)
    exit()

M = 96
for i in range(97, 97 + 26):
    if not chr(i) in S:
        M = i
        break
if M != 96:
    print(S + chr(M))
    exit()

unused = [False] * 128
for i in reversed(range(len(S))):
    for j in range(ord(S[i]) + 1, 97 + 26):
        if unused[j]:
            print(S[0:i] + chr(j))
            exit()
    unused[ord(S[i])] = True

Solution (Python):
S=input()
if S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
  exit()
alp='abcdefghijklmnopqrstuvwxyz'
N=len(S)
if N<26:
  T=S+alp
  i=0
  while T.index(alp[i])<N:
    i+=1
  print(S+alp[i])
  exit()
  
i=-1
nums=[alp.index(S[i])]
while max(nums)==nums[-1]:
  i-=1
  nums.append(alp.index(S[i]))

out=S[:-len(nums)]
tmp=26
for i in nums[:-1]:
  if i>nums[-1] and tmp>i:
    tmp=i
    
print(out+alp[tmp])

Solution (Python):
def main():
    from collections import Counter
    from string import ascii_lowercase

    S = input()

    ctr = Counter(S)
    for count in ctr.values():
        if count > 1:
            print(-1)
            return

    for char in ascii_lowercase:
        if ctr[char] == 0:
            print(S + char)
            return

    lis = list(S)
    stock = []
    ma = None
    while lis:
        x = lis.pop()
        if ma is None or ma < x:
            ma = x
        stock.append(x)
        if lis and lis[-1] < ma:
            stock.sort()
            for char in stock:
                if lis[-1] < char:
                    print(''.join(lis[:-1]) + char)
                    return
    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in alphabet:
    if i not in S:
        print(S + i)
        break
else:
    output = False
    while len(S) > 0 and not output:
        last = alphabet.index(S[-1])
        for i in alphabet[last + 1:]:
            if i not in S[:-1]:
                print(S[:-1] + i)
                output = not(output)
                break
        else:
            S = S[:-1]
    if not output:
        print(-1)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    
    S = Scanner.string()
    if len(S) == 26:
        if list(S) == list(reversed(list(string.ascii_lowercase))):
            print(-1)
            return
        i = 0
        for i in reversed(range(25)):
            if S[i] < S[i+1]:
                break
        ans = S[:i]
        k = S[i]
        for s in sorted(S[i+1:]):
            if k < s:
                ans += s
                break
        print(ans)
        return
    else:
        T = Counter(S)
        for c in string.ascii_lowercase:
            if T[c] == 0:
                print(S + c)
                return
    return


if __name__ == "__main__":
    main()

Solution (Python):
import string
S = input()
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        i = len(S) - 1
        while 0 <= i:
            if S[i-1] < S[i]:
                break
            i -= 1
        ans = ""
        for x in range(i):
            ans += S[x]

        for j in S[i:][::-1]:
            if ans[-1] < j:
                ans = ans[:-1] + j
                break

        print(ans)

    exit()

chars_set = set(S)
for c in string.ascii_lowercase:
    if c not in chars_set:
        S += c
        break
print(S)

Solution (Python):
S = list(input())

if len(S) < 26:
    W = list('abcdefghijklmnopqrstuvwxyz')
    for s in S:
        W.remove(s)
    S += W[0]
    print(*S, sep='')
else:
    s_last = S[-1]
    idx = -1
    for i in range(24, -1, -1):
        s = S[i]
        if s > s_last:
            s_last = s
        else:
            idx = i
            break
    if idx == -1:
        print(-1)
    else:
        res = []
        for s in S[idx+1:]:
            if s > S[idx]:
                res.append(s)
        res.sort()
        print(*(S[:idx] + [res[0]]), sep='')

Solution (Python):


S = input()
if len(S)<26:
    for i in [chr(i) for i in range(97, 97+26)]:
        if not i in S:
            print (S+i)
            exit()
else:
    
    for i in range(len(S)-1):


        if (S[-i-1] > S[-i-2]):
            for j in S[-i-1:][::-1]:
                
                if (S[-i-2]<j):
                    print (S[:-i-2]+j)
                    exit()
        else:
            pass
print (-1)

Solution (Python):
import sys

s = input()
if s == "zyxwvutsrqponmlkjihgfedcba":
	print(-1)
	sys.exit()
if len(s) < 26:
	flg = [0] * 26
	for i in range(len(s)):
		flg[ord(s[i])-97] = 1
	ans = s + chr(flg.index(0)+97)
	print(ans)

else:
	candidate = []
	for i in range(25,0,-1):
		candidate.append(s[i])
		if s[i-1] < s[i]:
			obj = s[i-1]
			ind = i-1
			break

	candidate.append(obj)
	candidate.sort()
	switch = candidate[candidate.index(obj)+1]
	ans = s[:ind] + switch
	print(ans)

Solution (Python):
from bisect import bisect
s=input()
n=len(s)
ls=[0]*26
if s == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  exit()
if n<26:
  for i in range(n):
    ls[ord(s[i])-97]=i+1
  for i in range(26):
    if ls[i]==0:
      x=i
      break
  print(s+chr(x+97))
else:
  for i in range(n):
    ls[i] = ord(s[i])-97
  if ls[25]>ls[24]:
    print(s[:24]+s[25])
  else:
    tmp = []
    for i in range(24,-1,-1):
      tmp.append(ls[i+1])
      if ls[i+1]>ls[i]:
        tmps = sorted(tmp)
        x = bisect(tmps,ls[i])
        print(s[:i]+chr(tmps[x]+97))
        break

Solution (Python):
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
S = [ord(a)-97 for a in S]
if len(S) < 26:
    for i in range(26):
        if i not in S:
            T = S + [i]
            print("".join([chr(a+97) for a in T]))
            exit()
else:
    for i in range(26)[::-1]:
        for j in range(S[i]+1, 26):
            if j in S[i:]:
                T = S[:i] + [j]
                print("".join([chr(a+97) for a in T]))
                exit()

Solution (Python):
s=input()
if len(s)!=26:
  for i in range(26):
    if chr(97+i) not in s:
      print(s+chr(97+i))
      break
else:
  for i in range(25)[::-1]:
    checking=s[i]
    for j in range(ord(checking)+1,123):
      if chr(j) not in s[:i]:
        print(s[:i]+chr(j))
        exit()
  print(-1)