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):
s = input()
alp = "abcdefghijklmnopqrstuvwxyz"
if len(s) == 26:
    bck = []
    for i in range(25):
        bck.append(s[25-i])
        if s[25-i] > s[24-i]:
            for j in range(alp.find(s[24-i]),26):
                if alp[j] in bck:
                    sn = alp[j]
                    break
            print(s[0:24-i]+sn)
            exit()
    print(-1)
else:
    for i in alp:
        if i in s:
            continue
        else:
            print(s+i)
            exit()

Solution (Python):
s = input() 
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

s=list(s)
oa = ord('a')
alp = [True]*26
for i in range(len(s)):
    alp[ord(s[i])-oa]=False

if len(s)<26:
    nokori_min =alp.index(True)
    add = chr(nokori_min + ord('a'))
    s+=add

    print(*s,sep='')
    exit()

nokori = []


from bisect import bisect_right

for i in reversed(range(len(s))):
    if ord(s[i-1]) > ord(s[i]):
        nokori.append(s.pop())
    else:
        if nokori == []:
            tmp = s.pop()
            s[-1] = tmp
            break
        else:

            nokori.append(s.pop())
            nokori.sort()
            index = bisect_right(nokori, s[-1])
            s[-1] = nokori[index]
            break

print(*s,sep='')

Solution (Python):

import numpy as np

S = input()
alphabet = np.zeros(26, dtype=int)
for i in S:
    alphabet[ord(i)-97]=1
if len(S)<26:
    add = alphabet.argmin()
    ans = S + chr(add+97)
else:
    i = 24
    while(i>=0 and S[i]>S[i+1]):
        i -= 1
    if i==-1:
        ans = '-1'
    else:
        j = 25
        while(i+1<j and S[i]>S[j]):
            j -= 1
        ans = S[:i] + S[j]

print(ans)

Solution (Python):
S = input()
li = [chr(ord('a') + i) for i in range(26)]
used = [0] * 26
dic = dict(zip(li, used))
for i in range(len(S)): dic[S[i]] = 1
if(len(S) < 26):
    for i in range(26):
        k = chr(ord('a') + i)
        v = dic[k]
        if(v == 0):
            print(S + k)
            exit(0)

if(len(S) == 26):
    for i in range(25, -1, -1):
        for j in range(26): dic[chr(ord('a') + j)] = 0
        for j in range(i): dic[S[j]] = 1
        add_c = ""
        for j in range(i + 1, 26):
            if(S[j] > S[i]):
                if(add_c == ""): add_c = S[j]
                else: add_c = min(add_c, S[j])
        if(add_c):
            print(S[:i] + add_c)
            exit(0)
print(-1)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    s = input()
    if len(s) < 26:
        al = [chr(ord('a') + i) for i in range(26)]
        for i in al:
            if not (i in s):
                print(s + i)
                exit()
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        exit()
    for i in range(25):
        if s[i] < s[i + 1]:
            k = i
    a = "z"
    for i in range(k + 1, 26):
        if s[k] < s[i]:
            a = min(a, s[i])
    print(s[:k] + a)

if __name__ == '__main__':
    main()

Solution (Python):
s = input()
t = set(list(s))
n = len(s)
al={chr(ord('a') + i) for i in range(26)}
if n != 26:
    p = al - t
    p = list(p)
    p.sort()
    print(s + p[0])
elif n == 26:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        now = s[25]
        p = []
        for i in reversed(range(26)):
            if ord(now) <= ord(s[i]):
                p.append(now)
                now = s[i]
            elif ord(now) > ord(s[i]):
                p.append(now)
                print(s[:i],end="")
                q = []
                for j in p:
                    if ord(s[i]) < ord(j):
                        q.append(ord(j))
                print(chr(min(q)))
                exit()

Solution (Python):
from heapq import heappush,heappop

S = input()
slist = "abcdefghijklmnopqrstuvwxyz"

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

lis =[]
T = S
if len(S)<26:
    Sset = set(S)
    for s in slist:
        if s not in Sset:
            print(S+s)
            exit()
else:
    while len(S)>0:
        lis.append(S[-1])
        lis.sort()
        S = S[:-1]
        for s in lis:
            if S + s > T:
                print(S + s)
                exit()

Solution (Python):
import collections
s = list(input())
sset = set(s)
if len(sset) != 26:
    for i in range(97,123):
        if chr(i) in sset:
            continue
        else:
            tmp = chr(i)
            s.append(tmp)
            break

    print(''.join(s))
    exit()
else:
    tmp = []
    flg = False
    
    for i in range(1,26):
        if s[i-1] < s[i]:
            tmp.append(i-1)
            continue
    
    if len(tmp) == 0:
        print(-1)
        exit()
    
    change_i = max(tmp)
    ans = s[:change_i]
    for i in range(97,123):
        if chr(i) in ans:
            continue
        else:
            if ord(s[change_i]) < i:
                ans.append(chr(i))
                break
    print(''.join(ans))

Solution (Python):
import collections
s = list(input())
sset = set(s)
if len(sset) != 26:
    for i in range(97,123):
        if chr(i) in sset:
            continue
        else:
            tmp = chr(i)
            s.append(tmp)
            break

    print(''.join(s))
    exit()
else:
    tmp = []
    flg = False
    
    for i in range(1,26):
        if s[i-1] < s[i]:
            tmp.append(i-1)
            continue
    
    if len(tmp) == 0:
        print(-1)
        exit()
    
    change_i = max(tmp)
    ans = s[:change_i]
    for i in range(97,123):
        if chr(i) in ans:
            continue
        else:
            if ord(s[change_i]) < i:
                ans.append(chr(i))
                break
    print(''.join(ans))

Solution (Python):
import collections
s = list(input())
sset = set(s)
if len(sset) != 26:
    for i in range(97,123):
        if chr(i) in sset:
            continue
        else:
            tmp = chr(i)
            s.append(tmp)
            break

    print(''.join(s))
    exit()
else:
    tmp = []
    flg = False
    for i in range(1,26):
        if s[i-1] < s[i]:
            tmp.append(i-1)
            continue

    if len(tmp) == 0:
        print(-1)
        exit()
    change_i = max(tmp)
    if change_i == 0:
        print(chr(ord(s[0])+1))
        exit()
    ans = s[:change_i]
    for i in range(97,123):
        if chr(i) in ans:
            continue
        else:
            if ord(s[change_i]) < i:
                ans.append(chr(i))
                break
    print(''.join(ans))

Solution (Python):
s = input()
a = [0 for i in range(26)]
for i in s:
	b = ord(i) - ord("a")
	a[b] += 1
for i in range(26):
	if a[i] == 0:
		x = chr(ord("a")+i)
		s += x
		print (s)
		exit()
a = [0 for i in range(26)]
for i,j in enumerate(reversed(s)):
	b = ord(j) - ord("a")
	a[b] += 1
	if i == 0: continue
	for k in range(b+1,26):
		if a[k] == 1:
			x = chr(ord("a")+k)
			ans = s[:(26-i)-1] + x
			print (ans)
			exit()
print (-1)

Solution (Python):
from sys import stdin
import sys



S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    sys.exit()
a = [chr(i) for i in range(97, 97+26)]
b = [0 for i in range(26)]
d = dict(zip(a, b))
d1 = d
for i, v in enumerate(S):
    d[v] = d.get(v, 0)+1
for i, v in enumerate(a):
    if d[v] != 1:
        print(S+v)
        sys.exit()
temp = []
temp1 = []

for i in range(25):
    temp1 = []
    temp.append(S[-1-i])
    
    for k, v in enumerate(temp):
        
        
        if S[-1-i-1] < v:
            temp1.append(v)
    
    if len(temp1) > 0:
        temp1.sort()
        print(S[:-1-i-1]+temp1[0])
        sys.exit()

Solution (Python):
S=input()
if len(S)<26:
    print(S+sorted(list({chr(i+97) for i in range(26)}-set(S)))[0])
else:
    tmp = [S[25]]
    for i in range(25):
        tmp.append(S[24-i])
        if S[24-i] < S[25-i]:
            tmp =sorted(tmp)
            print(S[:26-len(tmp)]+tmp[tmp.index(S[24-i])+1])
            exit()
    print(-1)

Solution (Python):
def main():
    s = input()
    al = list("abcdefghijklmnopqrstuvwxyz")
    d = set(s)
    n = len(s)
    if list(s) == al[::-1]:
        print(-1)
        return 0
    if n == 26:
        for i in (range(n-1))[::-1]:
            if s[i] < s[i+1]:
                for j in range(i+1,n)[::-1]:
                    if s[i] < s[j]:
                        print(s[:i]+s[j])
                        return 0
    else:
        for i in al:
            if i not in d:
                print(''.join(s)+i)
                return 0

if __name__ == '__main__':
    main()

Solution (Python):
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
for i in range(25):
    if s[i]<s[i+1]:
        k=i
a="z"
for i in range(k+1,26):
    if s[k]<s[i]:
        a=min(a,s[i])
print(s[:k]+a)

Solution (Python):
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
a=[]
for i in range(25,-1,-1):
    for j in range(24-(25-i),-1,-1):
        if s[j]<s[i]:
            a.append(s[:j]+s[i])
print(min(a))

Solution (Python):
s=input()
from bisect import bisect_left as bl,bisect_right as br
if len(s)==26:
  if s=="zyxwvutsrqponmlkjihgfedcba":print(-1);exit()
  f=s[-1]
  p=[s[-1]]
  for i in range(2,26):
    if s[-i]>f:
      f=s[-i];p.append(f)
    else:print(s[:-i]+p[bl(p,s[-i])]);exit()
  print(chr(ord(s[0])+1))
else:
  f=sorted(list(set(list("aqzxswedcvfrtgbnhyujmkilop"))-set(list(s))))
  print(s+f[0])

Solution (Python):
s = input()
if len(s)<26:
    seen = set()
    for c in s:
        seen.add(c)
    for i in range(26):
        tmp = chr(i+ord('a'))
        if tmp not in seen:
            print(s+tmp)
            break
else:
    pre = 'a'
    seen = set()
    for i, c in enumerate(reversed(s)):
        if c < pre:
            for j in range(26):
                tmp = chr(j+ord(c)+1)
                if tmp in seen:
                    print(s[:25-i]+tmp)
                    break
            break
        seen.add(c)
        pre = c
    else:
        print(-1)

Solution (Python):
import string
ABC=list(string.ascii_lowercase)
s=input()
S=list(s[::-1])

if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    if len(s)==26:
        for i in range(len(s)):
            n=ABC.index(S[i])
            if not set(ABC[n+1:])<=set(S[i+1:]):
                j=i
                break
        L=list(set(ABC[n+1:])-set(S[j+1:]))
        L.sort()
        sgyku=L[0:1]+S[j+1:]
        print("".join(sgyku[::-1]))
    else:
        L=list(set(ABC)-set(s))
        L.sort()
        print(s+L[0])

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    S = list(input())


    if len(S) == 26:
        
        
        for i in range(25, -1, -1):
            for j in range(25, i, -1):
                
                if S[i] < S[j]:
                    ans = S[:i] + [S[j]]
                    printlist(ans, '')
                    return

        print(-1)

    else:
        abc = [chr(i) for i in range(97, 123)]

        for s in S:
            if s in abc:
                abc[ord(s) - 97] = 0

        for t in abc:
            if t != 0:
                S.append(t)
                printlist(S, k='')
                return


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    S = list(input())

    abc = [chr(i) for i in range(97, 123)]
    

    for s in S:
        if s in abc:
            abc[ord(s) - 97] = 0



    if len(S) == 26:
        
        
        SR = S[::-1]
        
        idx = (INF, INF)
        for i in range(26):
            for j in range(i+1, 26):
                if SR[i] > SR[j]:
                    
                    
                    if j < idx[1]:
                        idx = (i, j)
                    break
        
        i, j = idx
        if i == INF:
            print(-1)
        else:
            ans = S[:-j-1]
            ans.append(SR[i])
            printlist(ans, '')

    else:
        for t in abc:
            if t != 0:
                S.append(t)
                printlist(S, k='')
                return

if __name__ == '__main__':
    solve()

Solution (Python):
import string
def main():
    s = input()
    if len(s) < 26:
        print(s + sorted(list(set(string.ascii_lowercase) - set(s)))[0])
    else:
        t = []
        for i in range(len(s)):
            te = []
            for j in range(i+1, len(s)):
                if s[i] < s[j]: te.append(s[j])
            te.sort()
            t.append(te)
        for i in range(len(s)):
            if t[-1-i]:
                print(s[:-1-i] + t[-1-i][0])
                break
        else: print(-1)
    return

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = list(input())
n = len(s)
d = list('abcdefghijklmnopqrstuvwxyz')
se = set(s)
if n < 26:
    for i,x in enumerate(d):
        if not x in se:
            s.append(x)
            break
    print(''.join(s))
else:
    for i in range(25)[::-1]:
        if s[i] < s[i+1]:
            tmp = s[i+1:n]
            tmp.sort()
            for j in tmp:
                if j > s[i]:
                    s[i] = j
                    break
            
            print(''.join(s[:i+1]))
            break
    else:
        print(-1)

Solution (Python):
S=input()
l=len(S)

if l <=25:
    for i in range(26):
        ch=chr(ord('a')+i)
        if ch not in S:
            S+=ch
            break
elif S=='zyxwvutsrqponmlkjihgfedcba':
    S=-1
else:
    index=24
    tmp=[ord(S[25])]
    while ord(S[index]) > ord(S[index+1]):
        tmp.append(ord(S[index]))
        index -= 1

    re=max(tmp)
    for ch in tmp:
        if ch < re and ch > ord(S[index]) :
            re = ch
    S=S[:index]+chr(re)

print(S)

Solution (Python):
S = input()

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

Solution (Python):
import sys
s = input()
counts = [0] * 26

for num in s:
    counts[ord(num) - 97] += 1
    
for i in range(26):
    if counts[i] == 0:
        print(s + chr(i + 97))
        sys.exit()

d = []
for i in reversed(range(len(s))):
    for num in d:
        if s[i] < num:
            print(s[0: i] + num)
            sys.exit()
        
    d.append(s[i])
    d.sort()

print(-1)

Solution (Python):
import string

S = input()
dictt = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13,
         'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
dic2 = ['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']

base = '0'
if len(S) < 26:
    for i in range(len(S)):
        bag = set()
        for s in S[:len(S)-i]:
            bag.add(dictt[s])
        for j in range(1, 27):
            if not j in bag:
                print(S+dic2[j-1])
                exit()
else:
    for i in range(len(S)):
        bag = set()
        for s in S[:len(S)-i]:
            bag.add(dictt[s])
        the_last_letter = (S[len(S)-i-1])
        avail_index = dictt[the_last_letter]
        for j in range(avail_index+1, 27):
            if not j in bag:
                print(S[:len(S)-i-1]+dic2[j-1])
                exit()
    print(-1)

Solution (Python):
import sys
from sys import stdin

s = stdin.readline().rstrip()
if len(s) == 26:
    count = 1
    for i in range(1, len(s)):
        if s[-i] < s[-i - 1]:
            count += 1
        else:
            break
    if count == 26:
        print(-1)
    else:
        for i in sorted(s[-count:]):
            if s[-count - 1] < i:
                print(s[:-count-1] + i)
                sys.exit()
else:
    d = {}
    for i in range(26):
        d.update({i: 0})
    for si in s:
        d[ord(si) - 97] += 1
    for i in range(26):
        if d[i] == 0:
            s += chr(i + 97)
            print(s)
            sys.exit()

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

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    S=readline().strip()
    N=len(S)
    alps=[chr(i) for i in range(97,97+26)]
    flags=[False]*26
    if N<26:
        for i in range(N):
            flags[ord(S[i])-97]=True
        for i in range(26):
            if flags[i]==False:
                s=alps[i]
                break
        print(S+s)

    else:
        if S=="zyxwvutsrqponmlkjihgfedcba":
            print(-1)
        else:
            k=0
            for i in range(26):
                if i==0:
                    j=ord(S[i])-97
                else:
                    if ord(S[i-1])-97<ord(S[i])-97:
                        k=i
                        j=ord(S[i])-97

            target1=S[k-1]
            for i in range(k,26):
                if i==k:
                    target2=S[k]
                else:
                    if ord(target1)-97<ord(S[i])-97<ord(target2)-97:
                        target2=S[i]

            print(S[:k-1]+target2)

if __name__=="__main__":
    main()

Solution (Python):

import sys

def main():
    S = input()
    T = 'abcdefghijklmnopqrstuvwxyz'
    if len(S) < 26:
        U = set(T) - set(S)
        print(S + sorted(U)[0])
    else:
        if S == T[::-1]:
            print(-1)
        else:
            count = 1
            for i in range(25):
                if S[25-i] > S[24-i]:
                    break
                else:
                    count += 1
            
            for s in sorted(S[26-count:]):
                if s > S[25-count]:
                    print(S[:25-count] + s)
                    exit()

if __name__ == '__main__':
    main()

Solution (Python):
s = input()
alp = ['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']
buf = ''
for i in alp:
    if not i in set(s):
        buf = i
        break
if buf:
    print(s+buf)
else:
    c = 1
    for i in range(1,len(s)):
        if s[-i-1]>s[-i]:
            c += 1
        else:break
    if c==26:
        print(-1)
    else:
        for i in sorted(s[-c:]):
            if ord(s[-c-1])<ord(i):
                buf = i
                break
        print(s[:-c-1]+buf)

Solution (Python):
import sys
from string import ascii_lowercase as AL
input = sys.stdin.readline


def main():
    S = input().rstrip()

    if len(S) < 26:
        for a in AL:
            if a not in S:
                ans = S + a
                break
    else:
        i = 25
        while True:
            if i == 0:
                ans = -1
                break
            if S[i-1] > S[i]:
                i -= 1
            else:
                idx = AL.index(S[i-1])
                while True:
                    idx += 1
                    if AL[idx] in S[i:]:
                        next_al = AL[idx]
                        break
                ans = S[:i-1] + next_al
                break
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
S = input()

if len(S) < 26:
    st = set(S)
    for c in range(26):
        if not chr(ord('a') + c) in st:
            S += chr(ord('a') + c)
            break
    print(S)
else:
    i = len(S) - 1
    while i - 1 >= 0 and ord(S[i]) < ord(S[i - 1]):
        i -= 1
    if i == 0:
        print(-1)
    else:
        next_chr = ord(S[i - 1]) - ord('a')
        st = set(S[i:])
        for c in range(next_chr + 1, 26):
            if chr(ord('a') + c) in st:
                print(S[:i - 1] + chr(ord('a') + c))
                break

Solution (Python):
s = input()
c = len(s)-1
r = 0
x = 97
while -2 < c:
    for i in range(x, 97+26):
        if chr(i) not in s:          
            s = s+chr(i)
            r = 1
            break
    if r == 1:
        break
    elif c < 0:
        break
    else:
        x = ord(s[-1]) +1
        c -= 1
        s = s[:len(s)-1]
if r==0:
    s = '-1'
print(s)

Solution (Python):
s = input()
c = len(s)-1
r = 0
x = 97
while -2 < c:
    for i in range(x, 97+26):
        if chr(i) not in s:          
            s = s+chr(i)
            r = 1
            break
    if r == 1:
        break
    elif c < 0:
        break
    else:
        x = ord(s[-1]) +1
        c -= 1
        s = s[:len(s)-1]
if r==0:
    s = '-1'
print(s)

Solution (Python):
s=str(input())
a=list("zyxwvutsrqponmlkjihgfedcba")
a=a[::-1]



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


if len(s)==26:
    s=s[::-1]
    for i in range(len(s)):
        for j in range(i+1):
            
            if s[i]<s[j]:
                s=s[::-1]
               
                print(s[:-(i+1)]+s[-(j+1)])
                exit()
else:
    for i in a:
        if i not in s:
            s+=i
            break
    print(s)

Solution (Python):
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
x=len(s)

al=[0]*26

if x!=26:
    for i in s:
        al[ord(i)-97]=1
    
    for i in range(26):
        if al[i]==0:
            s+=chr(97+i)
            print(s)
            exit()

else:
    option=[]
    
    for i in reversed(range(26)):
        option=sorted(option)
        num=ord(s[i])
        if i==25:
            option.append(num)
        else:
            if num>max(option):
                option.append(num)
            else:
                s=s[:26-len(option)-1]
                for i in option:
                    if i>num:
                        s+=chr(i)
                        print(s)
                        exit()

Solution (Python):
s=input()
a=97
if s=="zyxwvutsrqponmlkjihgfedcba":
    print("-1")
elif len(s)!=26:
    for i in range(26):
        if chr(a+i) not in s:
            print(s+chr(a+i))
            exit()
else:
    for i in range(26):
        for j in range(i+1):
            if s[-(i+1)]<s[-(j+1)]:
                print(s[:-(i+1)]+s[-(j+1)])
                exit()

Solution (Python):
s=input()
n=len(s)
cha=["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"]
if n<26:
    for i in range(26):
        if s.count(cha[i])==0:
            print(s+cha[i])
            exit()
else:
    for j in range(n-1,0,-1):
        if s[j]>s[j-1]:
            memo=j-1
            break
        if j==1:
            print(-1)
            exit()
    s2=s[:j]
    for i in range(26):
        if s2.count(cha[i])==0 and cha[i]>s[memo]:
            memo2=cha[i]
            break
    print(s[:j-1]+memo2)

Solution (Python):
S = input()

alf = set([chr(i) for i in range(ord('a'), ord('z')+1)])

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S) < 26:
    not_used_alf = list(alf - set(S))
    ans = S + min(not_used_alf)
    print(ans)
else:
    tmp = S[-1]
    for i in reversed(range(26)):
        if S[i] >= tmp:
            tmp = S[i]
            continue
        else:
            break
    tmp = set(S[i+1:])
    for x in [chr(i) for i in range(ord('a'), ord('z')+1)]:
        if x in tmp and x > S[i]:
            ans = S[:i] + x
            break
    print(ans)

Solution (Python):
S = input()

if len(S) < 26:
    for c in [chr(i) for i in range(ord('a'), ord('a') + 26)]:
        
        if c not in S:
            ans = S + c
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    for i in range(1, len(S)):
        j = - i
        if ord(S[j - 1]) < ord(S[j]):
            break
    
    change = 'z'
    for c in S[j:]:
        if ord(S[j - 1]) < ord(c) < ord(change):
            change = c
    ans = S[:j - 1] + change
print(ans)

Solution (Python):


 
S=list(input())
 
 
S=list(map(lambda x : ord(x)-97,S))
 
alpha=[0]*26
for s in S:
    alpha[s]=1
 
if len(S)!=26:
    for i in range(26):
        if alpha[i] == 0:
            S.append(i)
            ans=S[:]
            ans=list(map(lambda x :chr(x+97), ans))
            print("".join(ans))
            exit()
else:
    for i in range(26)[::-1]:
        if alpha[S[i]:].count(0)>0:
            for j in range(S[i],26):
                if alpha[j]==0:
                    ans=S[:i]+[j]
                    ans=list(map(lambda x :chr(x+97), ans))
                    print("".join(ans))
                    exit()
        else:
            alpha[S[i]]=0
 
print(-1)

Solution (Python):



S=list(input())


S=list(map(lambda x : ord(x)-97,S))

alpha=[0]*26
for s in S:
    alpha[s]=1

if len(S)!=26:
    for i in range(26):
        if alpha[i] == 0:
            S.append(i)
            ans=S[:]
            ans=list(map(lambda x :chr(x+97), ans))
            print("".join(ans))
            exit()
else:
    for i in range(26)[::-1]:
        if alpha[S[i]:].count(0)>0:
            for j in range(S[i],26):
                if alpha[j]==0:
                    ans=S[:i]+[j]
                    ans=list(map(lambda x :chr(x+97), ans))
                    print("".join(ans))
                    exit()
        else:
            alpha[S[i]]=0

print(-1)

Solution (Python):
s = input()
cnt = [0]*26
ans = ""
if s[0] == "z":
    print(-1)
    exit(0)
for i in range(len(s)):
    if cnt[int(ord(s[i])-ord("a"))] == 1:
        min = chr(ord("z")+1)
        for j in range(26):
            if cnt[j] == 0:
                if int(ord(s[i])) < ord(chr(j+ord("a"))) < ord(min):
                    min = chr(j+ord("a"))
        ans += min
        print(ans)
        exit(0)
    else :
        ans += s[i]
        cnt[int(ord(s[i])-ord("a"))] = 1
if ans == s :
    min = chr(ord("z") + 1)
    for j in range(26):
        if cnt[j] == 0:
            if ord(chr(j+ord("a"))) < ord(min):
                min = chr(j+ord("a"))
    if min == chr(ord("z") + 1):
        if s[0] == "z":
            print(-1)
            exit(0)
        else:
            for k in range(len(s)-1,-1,-1):
                ans = ans.strip(s[k])
                cnt[int(ord(s[k])-ord("a"))] -= 1
                if ord(s[k]) > ord(s[k-1]):
                    ans = ans.strip(s[k-1])
                    min = chr(ord("z") + 1)
                    for j in range(26):
                        if cnt[j] == 0:
                            if int(ord(s[k-1])) < ord(chr(j + ord("a"))) < ord(min):
                                min = chr(j + ord("a"))
                    ans += min
                    break
    else:
        ans += min
print(ans)

Solution (Python):
d="zyxwvutsrqponmlkjihgfedcba"
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":print(-1)
else:
    if len(s)!=26:
        for i in range(1,1+26):
            if d[-i] not in s:print(s+d[-i]);exit()
    else:
        from bisect import bisect_left as bl,bisect_right as br
        de=[s[-1]]
        for i in range(2,27):

            if de[-1]<s[-i]:de.append(s[-i])
            else:print(s[:-i]+de[bl(de,s[-i])]);exit()

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
s = list(s)
asc = range(97,123)

import bisect

if len(s) < 26:
    ss = [ord(u) for u in s]
    for i in asc:
        if i not in ss:
            s.append(chr(i))
            print(''.join(s))
            exit()
else:
    ss = s[::-1]
    a = [ord(ss[0])]
    for i in range(1,len(ss)):
        j = ord(ss[i])
        if j > a[-1]:
            a.append(j)
        else:
            k = bisect.bisect_right(a,j)
            x = chr(a[k])
            sss = s[:26-(i+1)] + [x]
            print(''.join(sss))
            exit()
    print(-1)

Solution (Python):
S = input()
if len(S) != 26:
    for i in range(26):
        if chr(ord('a') + i) not in S:
            print(S + chr(ord('a') + i))
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    can = []
    for i in range(26):
        if len(can) >= 1 and can[-1] > ord(S[-1-i]):
            for s in can:
                if s > ord(S[-1-i]):
                    print(S[:-1-i] + chr(s))
                    exit(0)
        can.append(ord(S[-1-i]))