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: Accepted)

//#pragma warning disable
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System;
using System.Numerics;
using System.Threading.Tasks;
using static System.Math;
using static System.Console;
class Z { static void Main() => new K(); }
class K
{
	int F() => int.Parse(ReadLine());
	long FL() => long.Parse(ReadLine());
	int[] G() => ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
	long[] GL() => ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
	public const int MOD = 1000000007;
	public K()
	{
		/*var N = len;
		var ls = new List<string>[N];
		for (var i = 0; i < N; i++) ls[i] = new List<string>();
		for (var i = 0; i < N; i++) ls[0].Add(i.ToString());
		for (var n = 1; n < N; n++) foreach (var s in ls[n - 1]) { var u = new HashSet<char>(s); for (var i = 0; i < N; i++) if (!u.Contains((char)(i + '0'))) ls[n].Add(s + i); }
		var l = new List<string>();
		foreach (var x in ls) l.AddRange(x);
		l.Sort(Compare);
		for (var i = 0; i < l.Count; i++)
		{
			if (i == 0) WriteLine($"{l[i]}");
			else
			{
				var t = Solve(l[i - 1]);
				WriteLine($"{l[i]}\t{t}\t{(l[i] == t ? "" : "ng")}");
			}
		}*/
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		WriteLine(Solve(ReadLine()));
		Out.Flush();
	}
	int Compare(string S, string T)
	{
		for (var i = 0; ; i++)
		{
			if (i >= S.Length && i >= T.Length) return 0;
			if (i >= S.Length) return -1;
			if (i >= T.Length) return 1;
			if (S[i] > T[i]) return 1;
			if (S[i] < T[i]) return -1;
		}
	}
	const char lower = 'a';
	const char upper = 'z';
	const int len = upper - lower + 1;
	string Solve(string S)
	{
		if (S.Length < len)
		{
			var used = new HashSet<char>(S);
			for (var c = lower; c <= upper; c++) if (!used.Contains(c)) return S + c;
		}
		for (var i = 0; i < len; i++) if (S[i] == upper)
			{
				var used = new HashSet<char>();
				for (var j = len - 1; j >= i + 2; j--)
					if (S[j - 1] < S[j])
					{
						for (var k = 0; k < j - 1; k++) used.Add(S[k]);
						for (var c = (char)(S[j - 1] + 1); c <= upper; c++) if (!used.Contains(c)) return S.Substring(0, j - 1) + c;
					}
				if (i == 0) break;
				for (var j = 0; j < i; j++) used.Add(S[j]);
				for (var c = (char)(S[i - 1] + 1); c <= upper; c++) if (!used.Contains(c)) return S.Substring(0, i - 1) + c;
			}
		return "-1";
	}
}