#include <fstream>
#include <vector>

using namespace std;

ifstream input("NCONHAU.inp");
ofstream output("NCONHAU.out");
int num = 0;
int ABS(int a)
{
	return (a >= 0) ? a : -a;
}

void print(int &n, vector <int> &sub)
{
	for (unsigned int i = 0; i < sub.size(); i++)
	{
		for (unsigned int j = 0; j < n; j++)
		{
			if (sub[i] != j)
			{
				output << 0 << ' ';
			}
			else
			{
				output << i + 1 << ' ';
			}
		}
		output << '\n';
	}
}

bool check(vector <int> &sub)
{
	for (unsigned int i = 0; i < sub.size() - 1; i++)
	{
		if (sub.back() == sub[i])
		{
			return false;
		}
		if (ABS(sub.back() - sub[i]) == sub.size() - 1 - i)
		{
			return false;
		}
	}
	return true;
}

void xephau(int &n, vector <int> &sub, int i = 0)
{
	if (i == n)
	{
		num++;
		output << num << '\n';
		print(n, sub);
		return;
	}
	for (int j = 0; j < n; j++)
	{
		sub.push_back(j);
		if (check(sub))
		{
			xephau(n, sub, i + 1);
		}
		sub.pop_back();
	}
}

int main()
{
	int n;
	input >> n;
	vector <int> sub;
	xephau(n, sub);
	input.close();
	output.close();
	return 0;
}