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 Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn get_seen(s: &[usize]) -> Vec<bool> {
    let mut seen = vec![false; 26 + 1];
    seen[0] = true;
    for i in 0..s.len() {
        if s[i] == 0 { return seen; }
        seen[s[i]] = true;
    }
    seen
}

fn print_str(s: &[usize]) {
    for i in 0..s.len() {
        if s[i] == 0 { break; }
        print!("{}", (s[i] as u8 -1 + 'a' as u8) as char);
    }
    println!();
}

fn main() {
    input!{
        ss: chars
    }
    let mut s = vec![0; 26];
    for i in 0..ss.len() {
        let c = ss[i];
        // 1-26
        let c = c as usize - 'a' as usize + 1;
        s[i] = c;
    }

    // 1
    if ss.len() < 26 {
        let idx = s.iter().position(|&v|v==0).unwrap();
        let value = get_seen(&s).iter().position(|&v|v==false).unwrap();
        s[idx] = value;
        print_str(&s);
    } else {
        let mut idx = s.len() - 1;
        let mut insert_idx = -1;
        let mut insert_val = 0;
        loop {
            let v = s[idx];
            for i in (0..idx).rev() {
                if s[i] < v {
                    insert_idx = i as isize;
                    insert_val = v;
                    break;
                }
            }
            if insert_idx != -1 {
                break;
            }
            idx -= 1;
            if idx == 0 { break; }
        }

        if insert_idx == -1 {
            return println!("{}", -1);
        }

        let idx = insert_idx as usize;
        s[idx] = insert_val;
        for i in idx+1..26 {
            s[i] = 0;
        }
        print_str(&s);
    }
}