#include <bits/stdc++.h>
using namespace std;

long max_num(long num) {
  int max_digit = -1,
      max_digit_indx = -1,
      l_indx = -1,
      r_indx = -1;

  string nis = to_string(num);
  for (long i = nis.size() - 1; i >= 0; i--) {

    if (nis[i] > max_digit) {
      max_digit = nis[i];
      max_digit_indx = i;
      continue;
    }

    if (nis[i] < max_digit) {
      l_indx = i;
      r_indx = max_digit_indx;
    }
  }

  if (l_indx == -1) return num;

  swap(nis[l_indx], nis[r_indx]);
  return stoi(nis);
}

int main() {
  long n;
  cin >> n;
  char c[n];
  for (long i = 0; i < n; i++) cin >> c[i];
  string str(c);
  string::iterator end_pos = std::remove(str.begin(), str.end(), ' ');
  str.erase(end_pos, str.end());
  str = to_string(max_num(stoi(str)));
  for (long i = 0; i < str.length(); i++) {
    std::cout << str.at(i) << endl;
  }
  return 0;
}