#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <stdio.h>

using namespace std;
#ifndef _USE_C_FUNCTION__
ifstream input("hoanvi.inp");
ofstream output("hoanvi.out");
#else
FILE *input = fopen("hoanvi.inp", "r");
FILE *output = fopen("hoanvi.out", "w");
#endif
void print(vector <int> &sub)
{
	for (vector<int>::iterator i = sub.begin(); i != sub.end(); i++)
	{
#ifdef _USE_C_FUNCTION__
		fprintf(output, "%u ", *i);
	}
	fprintf(output, "\n");
#else
	output << *i << ' ';
	}
	output << '\n';
#endif
}
#ifndef _USE_PRE_FUNCTION__
void hoanvi(vector <int> &sub)
{
	print(sub);
	vector<int>::iterator i, j;
	for (i = sub.end() - 2; i >= sub.begin(); i--)
	{
		if (*i < *(i + 1)) break;
		if (i == sub.begin())
		{
			return;
		}
	}
	for (j = sub.end() - 1; j >= sub.begin(); j--)
	{
		if (*i < *j)
		{
			swap(*i, *j);
			break;
		}
	}
	reverse(i + 1, sub.end());
	hoanvi(sub);
}

#else
void hoanvi(vector <int> &sub, vector <int> &end)
{
	while (sub != end)
	{
		print(sub);
		next_permutation(sub.begin(), sub.end());
	}
	print(end);
}
#endif
int main()
{
	auto start = chrono::high_resolution_clock::now();
	unsigned int n;
#ifndef _USE_C_FUNCTION__
	input >> n;
#else
	fscanf(input, "%u", &n);
#endif
	vector <int> sub(n), end(n);
	for (unsigned int i = 0; i < n; i++)
	{
		sub[i] = i + 1;
		end[i] = n - i;
	}
#ifdef _USE_PRE_FUNCTION__
	hoanvi(sub, end);
#else
	hoanvi(sub);
#endif
#ifndef _USE_C_FUNCTION__
	input.close();
	output.close();
#else
	fclose(input);
	fclose(output);
#endif
	auto stop = chrono::high_resolution_clock::now();
	auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
	cout << duration.count();
	cin.get();
	return 0;
}