#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void *a, const void *b) { return (*(char *)b - *(char *)a); }

int main() {
  int n;
  int i;
  scanf("%d", &n);
  char *niz1 = (char *)malloc(n * sizeof(char));
  char *niz2 = (char *)malloc(n * sizeof(char));
  int *chars = (int *)calloc(9, sizeof(int));

  for (i = 0; i < n; i++) {
    getchar();
    niz1[i] = niz2[i] = getchar();
    chars[niz1[i] - '0']++;
  }

  qsort(niz2, n, sizeof(char), cmpfunc);

  for (i = 0; i < n; i++)
    if (niz1[i] != niz2[i]) break;

  if (i == n) {
    char imaKopija = 0;
    for (int i = 0; i <= 9; i++)
      if (chars[i] > 1) imaKopija = 1;

    if (!imaKopija) {
      char pom = niz2[n - 2];
      niz2[n - 2] = niz2[n - 1];
      niz2[n - 1] = pom;
    }
    for (int i = 0; i < n; i++) {
      putchar(niz2[i]);
      putchar(' ');
    }

    return 0;
  }

  int j = n - 1;

  while (niz1[j] != niz2[i]) j--;
  niz1[j] = niz1[i];

  niz1[i] = niz2[i];
  i++;

  for (int j = 0; j < n; j++) {
    putchar(niz1[j]);
    putchar(' ');
  }
  return 0;
}