#include<iostream>
using namespace std;

char niz[1000000];
int N;
int max_ind = -1;
int L = -1, D = -1;
bool nz[10];
int i;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);
	
	cin >> N;
	for (i = 0; i < N; i++) cin >> niz[i];
	
	max_ind = N - 1;
	for (i = N-2; i >= 0; i--) if (niz[max_ind] < niz[i]) max_ind = i;
	else if (niz[max_ind] > niz[i]) L = i, D = max_ind;
		
	if (L == -1){
		for (i = 0; i < N; i++) if (!nz[niz[i]]) nz[niz[i]] = true;
		else {
			N = N-1;
			for (i = 0; i < N; i++) cout << niz[i] << ' ';
			cout << niz[i];
			return 0;
		}
		swap(niz[N-1],niz[N-2]); N = N-1;
		for (i = 0; i < N; i++) cout << niz[i] << ' ';
		cout << niz[i];
		return 0;
	}
	
	swap(niz[L], niz[D]);
	N = N-1;
	for (i = 0; i < N; i++) cout << niz[i] << ' ';
	cout << niz[i];
	return 0;
}