include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
const int N=11;
int n,k;
int a[N][N];
int res[N*N];int cnt=0;
bool c[N][N][N][N];
bool b[N][N];
bool flag=false;
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
void dfs(int x1,int y1,int k1){
if(flag==true) return;
if(cnt>=nn-1 && x1==n-1 && y1==n-1)
{
memset(c,0,sizeof c);
int x2=0,y2=0,x3=0,y3=0;
for(int i=0;i<nn-1;i++)
{
x3=x2+dx[res[i]],y3=y2+dy[res[i]];
if(res[i]==1)
{
if(c[x2-1][y2][x2][y2+1])//交叉了
return;
else{
c[x2][y2][x3][y3]=true;
c[x3][y3][x2][y2]=true;
}
}
else if(res[i]==3)
{
if(c[x2][y2+1][x2+1][y2])//交叉了
{
return;
}
else{
c[x2][y2][x3][y3]=true;
c[x3][y3][x2][y2]=true;
}
}
else if(res[i]==5)
{
if(c[x2+1][y2][x2][y2-1])//交叉了
{
return;
}
else{
c[x2][y2][x3][y3]=true;
c[x3][y3][x2][y2]=true;
}
}
else if(res[i]==7)
{
if(c[x2][y2-1][x2][y2+1])//交叉了
{
return;
}
else{
c[x2][y2][x3][y3]=true;
c[x3][y3][x2][y2]=true;
}
}
x2=x3,y2=y3;
}
flag=true;
for(int i=0;i<n*n-1;i++)
cout<<res[i];
cout<<endl;
}
for(int i=0;i<8;i++)
{
if(flag==true) return;
int x=x1+dx[i],y=y1+dy[i];
if( x<0 || x>=n || y<0 || y>=n || b[x][y]) continue;
if(a[x][y]==(k1+1)%k)
{
b[x][y]=true;
res[cnt++]=i;
dfs(x,y,a[x][y]);
if(flag==true) return;
cnt--;
b[x][y]=false;
}
}
return;
}
int main()
{
cin>>n>>k;
for(int i=0;i[HTML_REMOVED]>a[i][j];
b[0][0]=true;
dfs(0,0,0);
if(!flag) cout<<-1;
system("pause");
return 0;
}