#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int N=10005;
const int M=1005;
int prime[N],prime2[N];
int ans,cnt,ok[M],bas[M][4],map[4][4];
int hash2[10][10],hash3[10][10][10];
struct node{
char str[20],map[4][5];
}E[10*N];
struct Edge{
int v,next;
}edge[M];
int tot,head[10];
void add(int a,int b){
edge[tot].v=b;edge[tot].next=head[a];head[a]=tot++;
}
void build_map()
{
int i,l;