#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
next_permutation(a, a+n);
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CglpbnQgbjsKCWNpbiA+PiBuOwoJaW50IGFbbl07Cglmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgY2luID4+IGFbaV07CgluZXh0X3Blcm11dGF0aW9uKGEsIGErbik7Cglmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgY291dCA8PCBhW2ldIDw8ICIgIjsKCXJldHVybiAwOwp9