#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void print( vector<int> v )
{
    int n = v.size();
    for( int i = 0; i < n - 1; i++ )
    {
        cout << v[i] << " ";
    }
    cout << v[n-1] << endl;
}

int main()
{
    int n, m;
    vector<int> v;

    cin >> n;

    for( int i = 0; i < n; i++ )
    {
        cin >> m;
        v.push_back(m);
    }

    if( prev_permutation( v.begin(), v.end() ) == true )
        print(v);
    else
    {
        next_permutation( v.begin(), v.end() );
        print(v);
        if( next_permutation( v.begin(), v.end() ) == true )
            print(v);
        return 0;
    }

    if( next_permutation( v.begin(), v.end() ) == true )
        print(v);

    if( next_permutation( v.begin(), v.end() ) == true )
        print(v);

    return 0;
}
