#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 错排问题
int derangement(int n) {
if (n == 1) return 0;
if (n == 2) return 1;
return (n - 1) * (derangement(n - 1) + derangement(n - 2));
}
// 楼梯走法问题
void dfs(vector<int>& path, vector<vector<int>>& result, int n) {
if (path.size() == n) {
result.push_back(path);
return;
}
for (int i = 1; i <= n; ++i) {
if (find(path.begin(), path.end(), i) == path.end()) {
if (path.empty() || abs(i - path.back()) != path.size() + 1) {
path.push_back(i);
dfs(path, result, n);
path.pop_back();
}
}
}
}
int main() {
int n;
cin >> n;
// 错排问题
cout << derangement(n) << endl;
// 楼梯走法问题
vector<vector<int>> result;
vector<int> path;
dfs(path, result, n);
for (const auto& r : result) {
for (int i : r) {
cout << i;
}
cout << endl;
}
return 0;
}