Problem: Once upon a time, there was a king who loved beautiful costumes very much. The king had a special cocoon bed to make excellent cloth of silk. The cocoon bed had 16 small square rooms, forming a 4 × 4 lattice, for 16 silkworms. The cocoon bed can be depicted as follows:

<image>

The cocoon bed can be divided into 10 rectangular boards, each of which has 5 slits:

<image>

Note that, except for the slit depth, there is no difference between the left side and the right side of the board (or, between the front and the back); thus, we cannot distinguish a symmetric board from its rotated image as is shown in the following:

<image>

Slits have two kinds of depth, either shallow or deep. The cocoon bed should be constructed by fitting five of the boards vertically and the others horizontally, matching a shallow slit with a deep slit.

Your job is to write a program that calculates the number of possible configurations to make the lattice. You may assume that there is no pair of identical boards. Notice that we are interested in the number of essentially different configurations and therefore you should not count mirror image configurations and rotated configurations separately as different configurations.

The following is an example of mirror image and rotated configurations, showing vertical and horizontal boards separately, where shallow and deep slits are denoted by '1' and '0' respectively.

<image>

Notice that a rotation may exchange position of a vertical board and a horizontal board.



Input

The input consists of multiple data sets, each in a line. A data set gives the patterns of slits of 10 boards used to construct the lattice. The format of a data set is as follows:


XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX


Each x is either '0' or '1'. '0' means a deep slit, and '1' a shallow slit. A block of five slit descriptions corresponds to a board. There are 10 blocks of slit descriptions in a line. Two adjacent blocks are separated by a space.

For example, the first data set in the Sample Input means the set of the following 10 boards:

<image>

The end of the input is indicated by a line consisting solely of three characters "END".

Output

For each data set, the number of possible configurations to make the lattice from the given 10 boards should be output, each in a separate line.

Example

Input

10000 01000 00100 11000 01100 11111 01110 11100 10110 11110
10101 01000 00000 11001 01100 11101 01110 11100 10110 11010
END


Output

40
6

Solution (in CPP):

Solution:
#include<iostream>
#include<sstream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cassert>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)

using namespace std;
string cocoon[10];
string G[5];
set<string> usecase;
int ans;
int perm[5];

bool check(int p,int used)
{
  if(p >= 5)return true;

  rep(i,10)
    {
      if((used >> i) & 1)continue;

      bool ok = true;
      rep(j,5)
	{
	  //if(cocoon[j][perm[p]] == cocoon[i][j])
	  if(G[j][p] == cocoon[i][j])
	    {
	      ok = false;
	      break;
	    }
	}
      if(ok)
	if(check(p+1,used|(1<<i)))return true;
	
      ok = true;
      rep(j,5)
	{
	  //if(cocoon[j][perm[p]] == cocoon[i][4-j])
	  if(G[j][p] == cocoon[i][4-j])
	    {
	      ok = false;
	      break;
	    }
	}
      if(ok)
	if(check(p+1,used|(1<<i)))return true;
	
    }
  return false;
}

void dfs(int p,int used)
{
  if(p >= 5)
    {
      if(check(0,used))
	{
	  string use="";
	  rep(i,5)use += cocoon[perm[i]];
	  usecase.insert(use);
	}
      return;
    }

  rep(i,10)
    {
      if((used >> i) & 1)continue;
      perm[p] = i;
      G[p] = cocoon[i];
      dfs(p+1,used|(1<<i));

      reverse(cocoon[i].begin(),cocoon[i].end());
      G[p] = cocoon[i];
      dfs(p+1,used|(1<<i));
      reverse(cocoon[i].begin(),cocoon[i].end());
    }
}

int main()
{
  while(cin >> cocoon[0],cocoon[0][0] != 'E')
    {
      REP(i,1,10)cin >> cocoon[i];
      usecase.clear();

      dfs(0,0);
      cout << usecase.size()/8 << endl;
    }
  return 0;
}



/*
#include<iostream>
#include<sstream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cassert>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)

using namespace std;



string cocoon[10];
int perm[5];
char G[5][5];
set<string> usecase;
int ans;


void print()
{
  cout << "print----" << endl;

  rep(y,5)
    {
      rep(x,5)
	{
	  cout << G[y][x];
	  //assert(G[y][x] == '0' || G[y][x] == '1');
	}
      cout << endl;
    }
  cout << endl;
}

void rotate90(int x,int y,int a)
{
  int tmp[a+1][a+1];
  int X = x;
  REP(i,y,y+a)
    {
      REP(j,x,x+a)
	{
	  tmp[i-y][j-x] = G[(y+a-1)-(j-x)][X];
	}
      X++;
    }
  
  rep(i,a)
   rep(j,a)
    G[i+y][j+x] = tmp[i][j];
}

inline void revG(){ rep(y,5)rep(x,5)G[y][x] = (G[y][x]=='0'?'1':'0'); }

bool check(int used)
{
  revG();
  bool ok = false;
  rep(_,4)
    {
      if(!ok)
	{
	  string rem[5];
	  rep(x,5)rep(y,5)rem[x] += string(1,G[y][x]);

	  int store = used;
	  int counter = 0;
	  rep(i,5)
	    {
	      if(ok)break;
	      rep(j,10)
		{
		  if((used >> j) & 1)continue;
		  if(cocoon[j] == rem[i])
		    {
		      counter++;
		      used |= (1<<j);
		      break;
		    }
		}
	    }
	  if(counter == 5)
	      ok = true;
	    
	  used = store;
	}
      rotate90(0,0,5);
    }
  revG();
  return ok;
}

string itos(int i)
{
  stringstream ss; ss << i; return ss.str();
}

void dfs(int cnt,int used)
{
  
 if(cnt >= 5)
    {
      string use="";
      rep(y,5)
	{
	  use += itos(perm[y]);
	  //use += cocoon[perm[y]];
	  rep(x,5)G[y][x] = cocoon[perm[y]][x];
	}
      
      if(usecase.find(use) != usecase.end())return;
      if(!check(used))return;
	
      //cout << "ok!!" << endl;
      //print();

      rep(k,4)
	{
	  string dbg = use;
	  use = "";
	  rep(y,5)rep(x,5)use += string(1,G[y][x]);
	  //if(k == 0)assert(dbg == use);
	  usecase.insert(use);
	  rotate90(0,0,5);
	}
      ans++;
      return;
    }


  rep(i,10)
    {
      if((used >> i) & 1)continue;
      perm[cnt] = i;
      dfs(cnt+1,used|(1<<i));

      reverse(cocoon[i].begin(),cocoon[i].end());
      dfs(cnt+1,used|(1<<i));
      reverse(cocoon[i].begin(),cocoon[i].end());
    }

}

int main()
{
  while(cin >> cocoon[0],cocoon[0][0] != 'E')
    {
      ans = 0;
      usecase.clear();
      REP(i,1,10)cin >> cocoon[i];
      dfs(0,0);
      cout << ans << endl;
    }
  return 0;
}

*/

/*
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<iomanip>
#include<sstream>
#include<cassert>
#include<bitset>
#include<ctime>
#include<set>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)

using namespace std;
string cocoon[10];
string frame[5];
int cnt;

bool check(int phase,int type)
{
  bool f = phase%2;
  rep(i,5)
    {
      char c = (cocoon[type][i] == '1'?'0':'1');
      //cout << cocoon[type][i] << " c = " << c << " f = " << f << " frame[i][phase/2] = " << frame[i][phase/2] << endl;  
      if(f  && !((frame[i][phase/2] == c || frame[i][phase/2] == 'x')))return false;
      if(!f && !((frame[phase/2][i] == c || frame[phase/2][i] == 'x')))return false;
    }
  return true;
}
set<string> used;
void dfs(int phase,int p,string s)
{
  //cout << s  << " : " << phase << endl;
  if(phase >= 10)
    {
      //if(used.find(s) != used.end())return;
      //cout << s << endl;
      //used.insert(s);
      cnt++;
      //cout << cnt << endl;
      return;
    }
  rep(i,10)
    {
      if((p >> i) & 1)continue;
      if(phase%2)
	{
	  if(!check(phase,i))goto Skip3;
	  rep(j,5)frame[j][phase/2] = cocoon[i][j];
	  dfs(phase+1,p|(1<<i),s);
	  rep(j,5)frame[j][phase/2] = 'x';

	Skip3:;
	  string pres = cocoon[i];
	  reverse(cocoon[i].begin(),cocoon[i].end());	 
	  if(pres == cocoon[i])goto Skip4;
 	  if(!check(phase,i))goto Skip4;
	  rep(j,5)frame[j][phase/2] = cocoon[i][j];
	  dfs(phase+1,p|(1<<i),s);
	  rep(j,5)frame[j][phase/2] = 'x';

	Skip4:;
	  reverse(cocoon[i].begin(),cocoon[i].end());
	}
      else
	{
	  if(!check(phase,i))goto Skip;
	  frame[phase/2] = cocoon[i];
	  dfs(phase+1,p|(1<<i),s+" "+cocoon[i]);
	  frame[phase/2] = string(5,'x');
	  
	Skip:;
	  string pres = cocoon[i];
	  reverse(cocoon[i].begin(),cocoon[i].end());
	  if(pres == cocoon[i])goto Skip2;
	  if(!check(phase,i))goto Skip2;
	  frame[phase/2] = cocoon[i];
	  dfs(phase+1,p|(1<<i),s+" "+cocoon[i]);
	  frame[phase/2] = string(5,'x');
	  
	Skip2:;
	  reverse(cocoon[i].begin(),cocoon[i].end());
	}
    }
}

int main()
{

  string line;
  while(true)
    {
      cin >> line;
      if(line == "END")break;
      cnt = 0;
      cocoon[0] = line;
      REP(i,1,10)cin >> cocoon[i];
      rep(i,5)frame[i] = string(5,'x');
      string s = "";
      dfs(0,0,s);

      cout << cnt << endl;
    }
  return 0;
}
*/
/*
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<iomanip>
#include<sstream>
#include<cassert>
#include<bitset>
#include<set>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define _A_ true
using namespace std;
string cocoon[10];
string G[5];
set<string> mem;
int ans;

bool check(int used)
{
  string reg = G[0] + G[1] + G[2] + G[3] + G[4];
  if(mem.count(reg))return 0;//already counted
  mem.insert(reg);
  //cout << reg << endl;

  map<string,bool> rem;
  rep(i,10)
    {
      if((used >> i) & 1)continue;
      string vip = cocoon[i];
      rem[vip] = true;
      //cout << "in :: " << vip << endl;
      reverse(vip.begin(),vip.end());
      //cout << "in :: " << vip << endl;
      rem[vip] = true;
    }

  bool ch = true;
  string rG[5] = G;
 

  rep(i,5)
    rep(j,5)
      rG[i][j] = (G[j][i]=='1'?'0':'1');
 
  rep(i,5)
    {
      string px = rG[i];
      string rpx = px;
      reverse(rpx.begin(),rpx.end());
      if(!(rem[px] || rem[rpx]))
	{
	  return false;
	}
    }
  return true;
}

int dfs(int used,int p)
{
  if(p >= 5)
    {
      return check(used);
    }

  int ret = 0;
  rep(i,10)
    {
      if((used >> i) & 1)continue;
      G[p] = cocoon[i];
      ret += dfs(used|(1<<i),p+1);

      reverse(G[p].begin(),G[p].end());
      ret += dfs(used|(1<<i),p+1);
    }
  return ret;
}

int main()
{
  while(_A_)
    {
      string line;
      getline(cin,line);
      if(line == "END")break;
      mem.clear();
      ans = 0;
      stringstream ss;
      ss << line;
      for(int i=0;!(ss >> line).fail();i++)
	{
	  cocoon[i] = line;
	}
      cout << "-+-+-+-+-+-+-+-" << endl;
      cout << dfs(0,0) << endl;
    }
  return 0;
}

*/