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 JAVA):

Solution:
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main{

	Scanner sc=new Scanner(System.in);

	int INF=1<<28;
	double EPS=1e-9;

	int[] a, b, c, rev;
	int n, m, ans;

	void run(){
		// 10Â©ç5ÂIÔñP(10,5)=30240
		// 2^5=32p^[½]³¹éD½]³¹ÄàÏíçÈ¢àÌÍpX
		// 5Â©çÈép^[ÉÎ·éCp^[ðcè5ÂÅìêé©»è

		n=10;
		m=n/2;
		a=new int[n];
		rev=new int[1<<m];
		for(int i=0; i<1<<m; i++){
			int x=i;
			x=(x&0x55555555)<<1|(x>>1)&0x55555555;
			x=(x&0x33333333)<<2|(x>>2)&0x33333333;
			x=(x&0x0f0f0f0f)<<4|(x>>4)&0x0f0f0f0f;
			x=(x<<24)|((x&0xff00)<<8)|((x>>8)&0xff00)|(x>>24);
			rev[i]=(int)(x>>(32-m))&((1<<m)-1);
		}

		for(;;){
			for(int i=0; i<n; i++){
				String s=sc.next();
				if(s.equals("END")){
					return;
				}
				a[i]=Integer.parseInt(s, 2);
			}
			solve();
		}
	}

	void solve(){
		ans=0;
		int comb=(1<<m)-1;
		b=new int[m];
		c=new int[m];
		boolean[] used=new boolean[m];
		for(; comb<1<<n;){
			int div=1;
			for(int i=0, j=0, k=0; k<10; k++){
				if((comb>>k&1)==1){
					b[i++]=a[k];
					if(rev[a[k]]==a[k]){
						div*=2;
					}
				}else{
					c[j++]=a[k];
				}
			}

			Arrays.sort(b);
			int sum=0;
			for(;;){
				for(int sup=0; sup<1<<m; sup++){
					for(int i=0; i<m; i++){
						if((sup>>i&1)==1){
							b[i]=rev[b[i]];
						}
					}
					// bÌÀÑðcÅ\zÅ«é©
					Arrays.fill(used, false);
					sum++;
					// boolean f=true;
					for(int i=0; i<m; i++){
						int bits=0;
						for(int j=0; j<m; j++){
							bits=(bits<<1)|(b[j]>>i&1);
						}
						// debug(i, bits, Integer.toBinaryString(bits));

						int k=-1;
						for(int j=0; j<m; j++){
							if(!used[j]
									&&((c[j]^bits)==(1<<m)-1||(rev[c[j]]^bits)==(1<<m)-1)){
								k=j;
							}
						}

						if(k>=0){
							used[k]=true;
						}else{
							// f=false;
							sum--;
							break;
						}
					}

					for(int i=0; i<m; i++){
						if((sup>>i&1)==1){
							b[i]=rev[b[i]];
						}
					}
				}
				if(!nextPermutation(b)){
					break;
				}
			}

			if(sum!=0){
				// debug("sum", sum, "r", div);
			}
			ans+=sum/div;

			int x=comb&-comb, y=comb+x;
			comb=((comb&~y)/x>>1)|y;
		}
		// 8ÅêÎ¢¢ñ¶áËH
		ans/=8;
		// debug(ans);
		println(""+ans);
	}

	void swap(int[] is, int i, int j){
		int t=is[i];
		is[i]=is[j];
		is[j]=t;
	}

	void rev(int[] is, int i, int j){
		for(j--; i<j; i++, j--){
			int t=is[i];
			is[i]=is[j];
			is[j]=t;
		}
	}

	boolean nextPermutation(int[] is){
		int n=is.length;
		for(int i=n-1; i>0; i--){
			if(is[i-1]<is[i]){
				int j=n;
				while(is[i-1]>=is[--j]);
				swap(is, i-1, j);
				rev(is, i, n);
				return true;
			}
		}
		rev(is, 0, n);
		return false;
	}

	void debug(Object... os){
		System.err.println(Arrays.deepToString(os));
	}

	void print(String s){
		System.out.print(s);
	}

	void println(String s){
		System.out.println(s);
	}

	public static void main(String[] args){
		// System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
		new Main().run();
	}
}