import java.util.Scanner;
import java.util.Stack;
public class BOJ_10974 {
static int[] arr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
print();
while (true) {
if (nextPermutation())
continue;
else break;
}
}
private static boolean nextPermutation() {
int idx = arr.length - 1;
while (true) {
if (idx == 0) {
return false;
}
if (arr[idx - 1] > arr[idx]) {
idx -= 1;
continue;
}
int i = idx;
int j = -1;
for (int k = idx - 1; k < arr.length; k++) {
if (arr[idx - 1] < arr[k])
j = k;
}
if (j != -1) {
swap(i - 1, j);
reverse(i);
print();
break;
}
}
return true;
}
private static void print() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
if (i != arr.length - 1)
sb.append(" ");
}
System.out.println(sb.toString());
}
private static void reverse(int idx) {
Stack<Integer> stack = new Stack<>();
for (int i = idx; i < arr.length; i++) {
stack.add(arr[i]);
}
for (int i = idx; i < arr.length; i++) {
arr[i] = stack.pop();
}
}
private static void swap(int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}