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 in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace Atcoder {
    class Program {
        static void Main(string[] args) {
            var S = Console.ReadLine();

            var alp = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
            var lack = alp.Where(ch => !S.Contains(ch) && !string.IsNullOrEmpty(ch.ToString()));

            if (lack.Count() > 0) {
                Console.WriteLine(S + lack.First());
                return;
            }
            var S_ch = S.ToCharArray().ToList();

            var indexes = new List<int>();
            var index = 0;
            bool isOnce = true;
            for (int i = 0; i < S_ch.Count() - 1; i++) {
                if (getIndex(S_ch[i]) > getIndex(S_ch[i + 1])) {
                    if (isOnce) {
                        index = i - 1;
                        isOnce = false;
                    }
                } else
                    isOnce = true;
            }
            if (index == -1) {
                Console.WriteLine("-1");
                return;
            }

            S_ch = S_ch.Where(s => S_ch.IndexOf(s) <= index).ToList();

            //for (int i = 0; i < S_ch.Count(); i++) {
            //    if (i > index)
            //        S_ch.RemoveAt(i);
            //}

            //  S_ch[index] = getNext(S_ch[index]);
            //foreach (var i in indexes.OrderByDescending(i => i))
            //    S_ch.RemoveAt(i);

            //if (getIndex(S_ch[0]) > getIndex(getNext(S_ch[1])))
            //    S_ch[0] = getNext(S_ch[1]);
            //S_ch.RemoveAt(S_ch.Count() - 1);
            if (alp.Where(a => getIndex(a) > getIndex(S_ch.Last())).Count() > 0)
                S_ch[S_ch.Count() - 1] = alp.Where(a => getIndex(a) > getIndex(S_ch.Last())).First();

            var result = string.Empty;
            foreach (var s in S_ch)
                result += s;

            Console.WriteLine(result);
            return;
        }

        public static int getIndex(char c) {
            var alp = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
            return alp.ToList().IndexOf(c);
        }

        public static char getNext(char c) {
            var alp = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
            if (alp.Last() == c) {
                return c;
            } else {
                var index = alp.ToList().IndexOf(c);
                //while (index + 1 < alp.Count()) {
                var tmp = alp[index + 1];
                //if (!S.Contains(tmp))
                return tmp;
                //    index++;
                //}
                //return c;
            }
        }

        //static bool isVariable(char[] c) {

        //}

        static long max(long left, long right) {
            if (left >= right)
                return left;
            return right;
        }

        static long min(long left, long right) {
            if (left >= right)
                return right;
            return left;
        }
    }

    public static class Input {
        public static string Str => Console.ReadLine().Trim();
        public static int Int => int.Parse(Str);
        public static long Long => long.Parse(Str);
        public static double Double => double.Parse(Str);

        public static string[] StrArr => Str.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries).ToArray();
        public static int[] IntArr => StrArr.Select(int.Parse).ToArray();
        public static double[] DoubleArr => StrArr.Select(double.Parse).ToArray();
        public static long[] LongArr => StrArr.Select(long.Parse).ToArray();

        public static List<string> StrList => Str.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries).ToList();
        public static List<int> IntList => StrArr.Select(int.Parse).ToList();
        public static List<double> DoubleList => StrArr.Select(double.Parse).ToList();
        public static List<long> LongList => StrArr.Select(long.Parse).ToList();

        static bool eq<T, U>() => typeof(T).Equals(typeof(U));
        static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        static T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                            : eq<T, long>() ? ct<T, long>(long.Parse(s))
                            : eq<T, double>() ? ct<T, double>(double.Parse(s))
                            : eq<T, char>() ? ct<T, char>(s[0])
                                              : ct<T, string>(s);
        static void Multi<T>(out T a) => a = cv<T>(Str);
        static void Multi<T, U>(out T a, out U b) {
            var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        static void Multi<T, U, V>(out T a, out U b, out V c) {
            var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }

        static void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) {
            var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]);
        }
    }
}