#include <iostream>
#include <string>
using namespace std;
int main()
{
    unsigned long long int n;
    cin>>n;
    unsigned short int a[n];
    long long int ind=-1,maksInd=-1;
    short int maks=-1;
    for(unsigned long long int i=0;i<n;i++){
        cin>>a[i];
    }
    for(unsigned long long int i=n-1;i>0;i--)
    {
        if(a[i]>maks){
            maks=a[i];
            maksInd=i;
        }
        if(maks==9)
            break;
    }
    for(int i=0;i<=maksInd;i++)
    {
        if(a[i]<maks){
            swap(a[i],a[maksInd]);
            break;
        }
        else
        {
            if(i==maksInd)
            {
                swap(a[i-1],a[maksInd]);
            }
        }
    }
    for(int i=0;i<n;i++)
        cout<<a[i];
}