#include <bits/stdc++.h>

using namespace std;

void zameni(string &a, int i1, int i2)
{
    char t = a[i1];
    a[i1] = a[i2];
    a[i2] = t;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);

    int64_t n;
    cin >> n;

    string s;

    for(auto i = 0; i < n; i++)
    {
        char c;
        cin >> c;
        s += c;
    }

    vector<int64_t> u(n, -1);
    int64_t mx = n - 1;

    for(auto i = n - 2; i >= 0; i--)
    {
        if(s[i] < s[mx])
        {
            u[i] = mx;
        }
        else
        {
            mx = i;
            u[i] = -1;
        }
    }

    bool zamenjeno = false;
    for(auto i = 0; i < n; i++)
    {
        if(u[i] != -1)
        {
            zameni(s, i, u[i]);
            zamenjeno = true;
            break;
        }
    }

    if(!zamenjeno)
        zameni(s, n - 1, n - 2);

    for(auto i = 0; i < n; i++)
        cout << s[i] << ((i < s.length() - 1) ? ' ' : '\n');

    cout.flush();
    return 0;
}