#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 1000
#define M 1000
using namespace std;
int n,tot;
int a[N];
bool b[N],c[N],d[N];
void dfs(int deep)
{
if(deep>n)
{
tot++;
for(int i=1;i<=n;i++) cout<<a[i]<<' ';
cout<<endl;
return;
}
for(int i=1;i<=n;i++)
if(!b[i]&&!c[i-deep+100]&&!d[i+deep])
{
a[deep]=i;
b[i]=true;
c[i-deep+100]=true;
d[i+deep]=true;
dfs(deep+1);
b[i]=false;
c[i-deep+100]=false;
d[i+deep]=false;
}
}
int main()
{
cin>>n;
dfs(1);
cout<<tot<<endl;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 1000
#define M 1000
using namespace std;
int n,m;
int u,v;
int s,to[M],ne[M],pre[N];
void add(int u,int v)
{
to[++s]=v;
ne[s]=pre[u];
pre[u]=s;
}
int q[N],dis[N];
bool vi[N];
int main()
{
cin>>n>>m;
while(m--)
{
cin>>u>>v;
add(u,v);
}
int h=0,t=0;
q[++t]=1;
vi[1]=1;
dis[1]=0;
while(h<t)
{
int x=q[++h];
for(int i=pre[x];i;i=ne[i])
{
int y=to[i];
if(!vi[y])
{
vi[y]=1;
dis[y]=dis[x]+1;
q[++t]=y;
}
}
}
for(int i=1;i<=n;i++) cout<<i<<' '<<dis[i]<<endl;
}