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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

char table[20][6];

int ans;

void recursive(bool used[20],int yoko[5],int yoko_index,int tate[5],int tate_index,bool is_pre_yoko){

	if(is_pre_yoko == false && tate_index == 5){
		ans++;
		return;
	}

	if(is_pre_yoko == false){

		switch(yoko_index){
		case 0:
			for(int i = 0; i < 20; i++){
				if(used[i] == false){
					int next_yoko[5];
					next_yoko[0] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 1:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][1]-table[i][0]) == 1){
					int next_yoko[5];
					for(int k = 0; k < 1; k++)next_yoko[k] = yoko[k];
					next_yoko[1] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 2:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][2]-table[i][0]) == 1 &&  abs(table[tate[1]][2]-table[i][1]) == 1){
					int next_yoko[5];
					for(int k = 0; k < 2; k++)next_yoko[k] = yoko[k];
					next_yoko[2] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 3:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][3]-table[i][0]) == 1 &&  abs(table[tate[1]][3]-table[i][1]) == 1 &&
						abs(table[tate[2]][3]-table[i][2]) == 1 ){
					int next_yoko[5];
					for(int k = 0; k < 3; k++)next_yoko[k] = yoko[k];
					next_yoko[3] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		case 4:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[tate[0]][4]-table[i][0]) == 1 &&  abs(table[tate[1]][4]-table[i][1]) == 1 &&
						abs(table[tate[2]][4]-table[i][2]) == 1 && abs(table[tate[3]][4]-table[i][3]) == 1){
					int next_yoko[5];
					for(int k = 0; k < 4; k++)next_yoko[k] = yoko[k];
					next_yoko[4] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,next_yoko,yoko_index+1,tate,tate_index,true);
				}
			}
			break;
		}

	}else{

		switch(tate_index){
		case 0:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][0] - table[i][0]) == 1){
					int next_tate[5];
					next_tate[0] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 1:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][1] - table[i][0]) == 1 && abs(table[yoko[1]][1]-table[i][1]) == 1){
					int next_tate[5];
					for(int k = 0; k < 1; k++)next_tate[k] = tate[k];
					next_tate[1] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 2:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][2] - table[i][0]) == 1 && abs(table[yoko[1]][2]-table[i][1]) == 1 &&
						abs(table[yoko[2]][2]-table[i][2]) == 1){
					int next_tate[5];
					for(int k = 0; k < 2; k++)next_tate[k] = tate[k];
					next_tate[2] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 3:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][3] - table[i][0]) == 1 && abs(table[yoko[1]][3]-table[i][1]) == 1 &&
						abs(table[yoko[2]][3]-table[i][2]) == 1 && abs(table[yoko[3]][3]-table[i][3]) == 1){
					int next_tate[5];
					for(int k = 0; k < 3; k++)next_tate[k] = tate[k];
					next_tate[3] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		case 4:
			for(int i = 0; i < 20; i++){
				if(used[i] == false && abs(table[yoko[0]][4] - table[i][0]) == 1 && abs(table[yoko[1]][4]-table[i][1]) == 1 &&
						abs(table[yoko[2]][4]-table[i][2]) == 1 && abs(table[yoko[3]][4]-table[i][3]) == 1 && abs(table[yoko[4]][4]-table[i][4]) == 1){
					int next_tate[5];
					for(int k = 0; k < 4; k++)next_tate[k] = tate[k];
					next_tate[4] = i;
					bool next_used[20];
					for(int k = 0; k < 20; k++)next_used[k] = used[k];
					next_used[i] = true;
					if(i <= 9)next_used[i+10] = true;
					else{
						next_used[i-10] = true;
					}
					recursive(next_used,yoko,yoko_index,next_tate,tate_index+1,false);
				}
			}
			break;
		}
	}
}

void func(){

	for(int i = 1; i <= 9; i++)scanf("%s",table[i]);

	bool first_used[20];
	for(int i = 0; i < 20; i++)first_used[i] = false;

	for(int i = 0; i < 10; i++){
		for(int k = 0; k < 5; k++)table[i+10][k] = table[i][4-k];

		if(table[i][0] == table[i][4] && table[i][1] == table[i][3]){
			first_used[i+10] = true;
		}
	}

	ans = 0;

	int first_yoko[5],first_tate[5];

	recursive(first_used,first_yoko,0,first_tate,0,false);

	printf("%d\n",ans/8);
}

int main(){

	while(true){
		scanf("%s",table[0]);
		if(table[0][0] == 'E')break;

		func();
	}

	return 0;
}