Language: C++

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back

void Yes(){
	cout<<"Yes"<<endl;
	exit(0);
}
 
void No(){
	cout<<"No"<<endl;
	exit(0);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
    string S; cin >> S;
    if(S == "zyxwvutsrqponmlkjihgfedcba") {
        cout << -1 << endl;
        return 0;
    }
    map<char, int> mp;
    int N = S.size();
    for(int i = 0; i < S.size(); i++) {
        mp[S[i]] ++;
    }
    if(N != 26) {
        rep(i, 26) {
            if(mp['a' + i] == 0) {
                S += ('a' + i);
                break;
            }
        }
        cout << S << endl;
        return 0;
    } else {
        set<pair<char, int>> s;
        per(i, N) {
            if(S[i] < S[i - 1]) {
                s.insert(pair<char, int> (S[i], i));
            } else {
                char t = S[i];
                S[i] = (*(s.upper_bound(S[i])).fr;
                S[(*(s.upper_bound(S[i])).sc] = t;
                cout << S << endl;
                return 0;
            }
        }
    }
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:82:40: error: no matching function for call to 'std::set<std::pair<char, int> >::upper_bound(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)'
   82 |                 S[i] = (*(s.upper_bound(S[i])).fr;
      |                           ~~~~~~~~~~~~~^~~~~~
In file included from /usr/include/c++/14/set:63,
                 from a.cc:13:
/usr/include/c++/14/bits/stl_set.h:871:9: note: candidate: 'template<class _Kt> decltype ((std::set<_Key, _Compare, _Alloc>::iterator)(((std::set<_Key, _Compare, _Alloc>*)this)->std::set<_Key, _Compare, _Alloc>::_M_t._M_upper_bound_tr(__x))) std::set<_Key, _Compare, _Alloc>::upper_bound(const _Kt&) [with _Key = std::pair<char, int>; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >]'
  871 |         upper_bound(const _Kt& __x)
      |         ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:871:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_set.h: In substitution of 'template<class _Kt> decltype ((std::set<std::pair<char, int> >::iterator)(((std::set<std::pair<char, int> >*)this)->std::set<std::pair<char, int> >::_M_t.std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::_M_upper_bound_tr(__x))) std::set<std::pair<char, int> >::upper_bound(const _Kt&) [with _Kt = char]':
a.cc:82:40:   required from here
   82 |                 S[i] = (*(s.upper_bound(S[i])).fr;
      |                           ~~~~~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_set.h:872:52: error: no matching function for call to 'std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::_M_upper_bound_tr(const char&)'
  872 |         -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
      |                              ~~~~~~~~~~~~~~~~~~~~~~^~~~~
In file included from /usr/include/c++/14/map:62,
                 from a.cc:12:
/usr/include/c++/14/bits/stl_tree.h:1347:9: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_upper_bound_tr(const _Kt&) [with _Req = _Kt; _Key = std::pair<char, int>; _Val = std::pair<char, int>; _KeyOfValue = std::_Identity<std::pair<char, int> >; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >]'
 1347 |         _M_upper_bound_tr(const _Kt& __k)
      |         ^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1347:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h: In substitution of 'template<class _Func, class _SfinaeType> using std::__has_is_transparent_t = typename std::__has_is_transparent<_Func, _SfinaeType>::type [with _Func = std::less<std::pair<char, int> >; _SfinaeType = char]':
/usr/include/c++/14/bits/stl_tree.h:1345:9:   required by substitution of 'template<class _Kt> decltype ((std::set<std::pair<char, int> >::iterator)(((std::set<std::pair<char, int> >*)this)->std::set<std::pair<char, int> >::_M_t.std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::_M_upper_bound_tr(__x))) std::set<std::pair<char, int> >::upper_bound(const _Kt&) [with _Kt = char]'
 1345 |                typename _Req = __has_is_transparent_t<_Compare, _Kt>>
      |                ^~~~~~~~
a.cc:82:40:   required from here
   82 |                 S[i] = (*(s.upper_bound(S[i])).fr;
      |                           ~~~~~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_function.h:1427:11: error: no type named 'type' in 'struct std::__has_is_transparent<std::less<std::pair<char, int> >, char, void>'
 1427 |     using __has_is_transparent_t
      |           ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h: In substitution of 'template<class _Kt> decltype ((std::set<std::pair<char, int> >::iterator)(((std::set<std::pair<char, int> >*)this)->std::set<std::pair<char, int> >::_M_t.std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::_M_upper_bound_tr(__x))) std::set<std::pair<char, int> >::upper_bound(const _Kt&) [with _Kt = char]':
a.cc:82:40:   required from here
   82 |                 S[i] = (*(s.upper_bound(S[i])).fr;
      |                           ~~~~~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_tree.h:1356:9: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_upper_bound_tr(const _Kt&) const [with _Req = _Kt; _Key = std::pair<char, int>; _Val = std::pair<char, int>; _KeyOfValue = std::_Identity<std::pair<char, int> >; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >]'
 1356 |         _M_upper_bound_tr(const _Kt& __k) const
      |         ^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1356:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_set.h:877:9: note: candidate: 'template<class _Kt> decltype ((std::set<_Key, _Compare, _Alloc>::iterator)(((const std::set<_Key, _Compare, _Alloc>*)this)->std::set<_Key, _Compare, _Alloc>::_M_t._M_upper_bound_tr(__x))) std::set<_Key, _Compare, _Alloc>::upper_bound(const _Kt&) const [with _Key = std::pair<char, int>; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >]'
  877 |         upper_bound(const _Kt& __x) const
      |         ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:877:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_set.h: In substitution of 'template<class _Kt> decltype ((std::set<std::pair<char, int> >::iterator)(((const std::set<std::pair<char, int> >*)this)->std::set<std::pair<char, int> >::_M_t.std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::_M_upper_bound_tr(__x))) std::set<std::pair<char, int> >::upper_bound(const _Kt&) const [with _Kt = char]':
a.cc:82:40:   required from here
   82 |                 S[i] = (*(s.upper_bound(S[i])).fr;
      |                           ~~~~~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_set.h:878:52: error: no matching function for call to 'std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::_M_upper_bound_tr(const char&) const'
  878 |         -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
      |                              ~~~~~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:1347:9: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_upper_bound_tr(const _Kt&) [with _Req = _Kt; _Key = std::pair<char, int>; _Val = std::pair<char, int>; _KeyOfValue = std::_Identity<std::pair<char, int> >; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >]'
 1347 |         _M_upper_bound_tr(const _Kt& __k)
      |         ^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1347:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_tree.h:1356:9: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_upper_bound_tr(const _Kt&) const [with _Req = _Kt; _Key = std::pair<char, int>; _Val = std::pair<char, int>; _KeyOfValue = std::_Identity<std::pair<char, int> >; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >]'
 1356 |         _M_upper_bound_tr(const _Kt& __k) const
      |         ^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1356:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_set.h:861:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::upper_bound(const key_type&) [with _Key = std::pair<char, int>; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >; iterator = std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::const_iterator; key_type = std::pair<char, int>]'
  861 |       upper_bound(const key_type& __x)
      |       ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:861:35: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char'} to 'const std::set<std::pair<char, int> >::key_type&' {aka 'const std::pair<char, int>&'}
  861 |       upper_bound(const key_type& __x)
      |                   ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:865:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::const_iterator std::set<_Key, _Compare, _Alloc>::upper_bound(const key_type&) const [with _Key = std::pair<char, int>; _Compare = std::less<std::pair<char, int> >; _Alloc = std::allocator<std::pair<char, int> >; const_iterator = std::_Rb_tree<std::pair<char, int>, std::pair<char, int>, std::_Identity<std::pair<char, int> >, std::less<std::pair<char, int> >, std::allocator<std::pair<char, int> > >::const_iterator; key_type = std::pair<char, in