#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long i, n, f; cin >> n;
    int j, pre;
    vector<int> polica(n), cifra(10, -1);
    bool h = true, dve_iste = false;

    scanf("%d", &(polica[0]));
    cifra[polica[0]] = 0;
    pre = polica[0];
    for(i = 1; i < n; ++i)
    {
        scanf("%d", &(polica[i]));
        if(cifra[polica[i]] != -1) dve_iste = true;
        cifra[polica[i]] = i;
        if(polica[i] > pre) h = false;
        pre = polica[i];
    }

    if(h) {if(dve_iste == false) swap(polica[n - 2], polica[n - 1]);}

    else
    {
        pre = 10;
        i = 0;
        f = n - 2;

        while(i < f)
        {
            if(pre > polica[i])
            {
                j = 9;
                while(j >= 0 && polica[i] < j)
                {
                    if(i < cifra[j])
                    {
                        swap(polica[i], polica[cifra[j]]);
                        h = true;
                        break;
                    }
                    --j;
                }
            }
            if(h == true) break;
            pre = polica[i];
            ++i;
        }
        if(i == f) swap(polica[i], polica[i + 1]);
    }

    for(i = 0; i < n; ++i) printf("%d ", polica[i]);

    return 0;
}