Problem: In AD 3456, the earth is too small for hundreds of billions of people to live in peace. Interstellar Colonization Project with Cubes (ICPC) is a project that tries to move people on the earth to space colonies to ameliorate the problem. ICPC obtained funding from governments and manufactured space colonies very quickly and at low cost using prefabricated cubic blocks.

The largest colony looks like a Rubik's cube. It consists of 3 × 3 × 3 cubic blocks (Figure J.1A). Smaller colonies miss some of the blocks in the largest colony.

When we manufacture a colony with multiple cubic blocks, we begin with a single block. Then we iteratively glue a next block to existing blocks in a way that faces of them match exactly. Every pair of touched faces is glued.

<image>


Figure J.1: Example of the largest colony and a smaller colony

However, just before the first launch, we found a design flaw with the colonies. We need to add a cable to connect two points on the surface of each colony, but we cannot change the inside of the prefabricated blocks in a short time. Therefore we decided to attach a cable on the surface of each colony. If a part of the cable is not on the surface, it would be sheared off during the launch, so we have to put the whole cable on the surface. We would like to minimize the lengths of the cables due to budget constraints. The dashed line in Figure J.1B is such an example.

Write a program that, given the shape of a colony and a pair of points on its surface, calculates the length of the shortest possible cable for that colony.



Input

The input contains a series of datasets. Each dataset describes a single colony and the pair of the points for the colony in the following format.

x1 y1 z1 x2 y2 z2
b0,0,0b1,0,0b2,0,0
b0,1,0b1,1,0b2,1,0
b0,2,0b1,2,0b2,2,0
b0,0,1b1,0,1b2,0,1
b0,1,1b1,1,1b2,1,1
b0,2,1b1,2,1b2,2,1
b0,0,2b1,0,2b2,0,2
b0,1,2b1,1,2b2,1,2
b0,2,2b1,2,2b2,2,2


(x1, y1, z1) and (x2, y2, z2) are the two distinct points on the surface of the colony, where x1, x2, y1, y2, z1, z2 are integers that satisfy 0 ≤ x1, x2, y1, y2, z1, z2 ≤ 3. bi,j,k is '#' when there is a cubic block whose two diagonal vertices are (i, j, k) and (i + 1, j + 1, k + 1), and bi,j,k is '.' if there is no block. Figure J.1A corresponds to the first dataset in the sample input, whereas Figure J.1B corresponds to the second. A cable can pass through a zero-width gap between two blocks if they are touching only on their vertices or edges. In Figure J.2A, which is the third dataset in the sample input, the shortest cable goes from the point A (0, 0, 2) to the point B (2, 2, 2), passing through (1, 1, 2), which is shared by six blocks. Similarly, in Figure J.2B (the fourth dataset in the sample input), the shortest cable goes through the gap between two blocks not glued directly. When two blocks share only a single vertex, you can put a cable through the vertex (Figure J.2C; the fifth dataset in the sample input).

You can assume that there is no colony consisting of all 3 × 3 × 3 cubes but the center cube.

Six zeros terminate the input.

<image>


Figure J.2: Dashed lines are the shortest cables. Some blocks are shown partially transparent for illustration.

Output

For each dataset, output a line containing the length of the shortest cable that connects the two given points. We accept errors less than 0.0001. You can assume that given two points can be connected by a cable.

Examples

Input

0 0 0 3 3 3
###
###
###
###
###
###
###
###
###
3 3 0 0 0 3
#..
###
###
###
###
###
#.#
###
###
0 0 2 2 2 2
...
...
...
.#.
#..
...
##.
##.
...
0 1 2 2 1 1
...
...
...
.#.
#..
...
##.
##.
...
3 2 0 2 3 2
###
..#
...
..#
...
.#.
..#
..#
.##
0 0 0 0 0 0


Output

6.70820393249936941515
6.47870866461907457534
2.82842712474619029095
2.23606797749978980505
2.82842712474619029095


Input

0 0 0 3 3 3









3 3 0 0 0 3
..





.#


0 0 2 2 2 2
...
...
...
.#.
..
...
.
.
...
0 1 2 2 1 1
...
...
...
.#.
..
...
.
.
...
3 2 0 2 3 2

..#
...
..#
...
.#.
..#
..#
.##
0 0 0 0 0 0


Output

6.70820393249936941515
6.47870866461907457534
2.82842712474619029095
2.23606797749978980505
2.82842712474619029095

Solution (in CPP):

Solution:
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
char in[3][3][4];
int DIV=7;
int gcd(int a,int b){
	while(a){
		b%=a;int c=a;a=b;b=c;
	}return b;
}
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a){return max(a,-a);}
struct Pt {
	double x, y,z;
	Pt() {}
	Pt(double x, double y,double z) : x(x), y(y),z(z) {}
	Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y,z+a.z); }
	Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y,z-a.z); }
//	Pt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }
	Pt operator-() const { return Pt(-x, -y,-z); }
	Pt operator*(const double &k) const { return Pt(x * k, y * k,z*k); }
	Pt operator/(const double &k) const { return Pt(x / k, y / k,z/k); }
	double ABS() const { return sqrt(x * x + y * y+z*z); }
	double abs2() const { return x * x + y * y+z*z; }
//	double arg() const { return atan2(y, x); }
	double dot(const Pt &a) const { return x * a.x + y * a.y+z*a.z; }
//	double det(const Pt &a) const { return x * a.y - y * a.x; }
};
double ijk[110000];
int v[110000];
int can[4][4][4];
int canx[4][4][4];
int cany[4][4][4];
int canz[4][4][4];
int canxy[4][4][4];
int canyz[4][4][4];
int canzx[4][4][4];
int rev[110000];
char ss[4];
int main(){
	int a,b,c,d,e,f;
	while(scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f),a+b+c+d+e+f){
		for(int i=0;i<3;i++)for(int j=0;j<3;j++){
			scanf("%s",ss);
			for(int k=0;k<3;k++)in[k][j][i]=ss[k];
		}
		vector<Pt> hb;
		for(int i=0;i<4;i++)for(int j=0;j<4;j++)for(int k=0;k<4;k++){
			can[i][j][k]=canx[i][j][k]=cany[i][j][k]=canz[i][j][k]=0;
			canxy[i][j][k]=canyz[i][j][k]=canzx[i][j][k]=0;
		}
		for(int i=0;i<4;i++)for(int j=0;j<4;j++)for(int k=0;k<4;k++){
			if(i==0||i==3||j==0||j==3||k==0||k==3)can[i][j][k]=2;
			if(j==0||j==3||k==0||k==3)canx[i][j][k]=2;
			if(i==0||i==3||k==0||k==3)cany[i][j][k]=2;
			if(i==0||i==3||j==0||j==3)canz[i][j][k]=2;
			if(k==0||k==3)canxy[i][j][k]=2;
			if(i==0||i==3)canyz[i][j][k]=2;
			if(j==0||j==3)canzx[i][j][k]=2;
		}
		for(int i=0;i<3;i++)for(int j=0;j<3;j++)for(int k=0;k<3;k++){
			int key=0;
			if(in[i][j][k]=='#'){
				key=1;
			}else{
				key=2;
			}
			for(int l=0;l<8;l++){
				can[i+l/4][j+l%4/2][k+l%2]|=key;
			}
			for(int l=0;l<4;l++){
				canx[i][j+l/2][k+l%2]|=key;
				cany[i+l/2][j][k+l%2]|=key;
				canz[i+l/2][j+l%2][k]|=key;
			}
			for(int l=0;l<2;l++){
				canxy[i][j][k+l%2]|=key;
				canyz[i+l%2][j][k]|=key;
				canzx[i][j+l%2][k]|=key;
			}
		}
		/*for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				for(int k=0;k<4;k++)printf("%d ",can[k][j][i]);
				printf("\n");
			}
			printf("\n");
		}*/
		for(int i=0;i<=3;i++){
			for(int j=0;j<=3;j++){
				for(int k=0;k<=3;k++){
					if(can[i][j][k]==3)hb.push_back(Pt((double)i,(double)j,(double)k));
				}
				for(int k=0;k<3;k++){
					for(int l=2;l<=DIV;l++){
						for(int m=1;m<l;m++){
							if(gcd(m,l)!=1)continue;
							if(canz[i][j][k]==3)hb.push_back(Pt((double)i,(double)j,k+(double)m/l));
							if(cany[i][k][j]==3)hb.push_back(Pt((double)i,k+(double)m/l,(double)j));
							if(canx[k][i][j]==3)hb.push_back(Pt(k+(double)m/l,(double)i,(double)j));
						}
					}
				}
			}
		}
		int sz=hb.size();
		for(int i=0;i<sz;i++){
			v[i]=0;
			ijk[i]=99999999;
		}
		Pt start=Pt(a,b,c);
		Pt goal=Pt(d,e,f);
		for(int i=0;i<sz;i++){
			if((hb[i]-start).ABS()<EPS){
				ijk[i]=0;
			}
		}
		for(int i=0;i<sz;i++){
			int at=0;
			double dist=999999999;
			for(int j=0;j<sz;j++){
				if(!v[j]&&dist>ijk[j]){
					dist=ijk[j];at=j;
				}
			}
			v[at]=1;
		//	printf("%f %f %f: %f\n",hb[at].x,hb[at].y,hb[at].z,ijk[at]);
			if((hb[at]-goal).ABS()<EPS){
				printf("%.12f\n",dist);
				int cur=at;
				//while((hb[cur]-start).ABS()>EPS){
				//	cur=rev[cur];
				//	printf("%f %f %f: %f\n",hb[cur].x,hb[cur].y,hb[cur].z,ijk[cur]);
				//}
				break;
			}
			for(int j=0;j<sz;j++){
				if(v[j])continue;
				bool X=false;
				bool Y=false;
				bool Z=false;
				int xx=(int)(min(hb[j].x,hb[at].x)+EPS);
				int yy=(int)(min(hb[j].y,hb[at].y)+EPS);
				int zz=(int)(min(hb[j].z,hb[at].z)+EPS);
				if(ABS(hb[j].x-hb[at].x)<EPS&&ABS(hb[j].x-xx)<EPS)X=true;
				if(ABS(hb[j].y-hb[at].y)<EPS&&ABS(hb[j].y-yy)<EPS)Y=true;
				if(ABS(hb[j].z-hb[at].z)<EPS&&ABS(hb[j].z-zz)<EPS)Z=true;
				if(!X&&!Y&&!Z)continue;
				if(!X&&(int)(min(hb[j].x+1,hb[at].x+1)+EPS)!=(int)(max(hb[at].x+1,hb[j].x+1)-EPS))continue;
				if(!Y&&(int)(min(hb[j].y+1,hb[at].y+1)+EPS)!=(int)(max(hb[at].y+1,hb[j].y+1)-EPS))continue;
				if(!Z&&(int)(min(hb[j].z+1,hb[at].z+1)+EPS)!=(int)(max(hb[at].z+1,hb[j].z+1)-EPS))continue;
				bool can=false;
				bool Xs=false;
				bool Ys=false;
				bool Zs=false;
				if(ABS(hb[j].x-(int)(hb[j].x+EPS))<EPS)Xs=true;
				if(ABS(hb[j].y-(int)(hb[j].y+EPS))<EPS)Ys=true;
				if(ABS(hb[j].z-(int)(hb[j].z+EPS))<EPS)Zs=true;
				if(X&&canyz[xx][yy][zz]==3)can=true;
				if(Y&&canzx[xx][yy][zz]==3)can=true;
				if(Z&&canxy[xx][yy][zz]==3)can=true;
				if(X&&Y&&Xs&&Ys&&canz[xx][yy][zz]==3)can=true;
				if(Z&&Y&&Zs&&Ys&&canx[xx][yy][zz]==3)can=true;
				if(X&&Z&&Xs&&Zs&&cany[xx][yy][zz]==3)can=true;
				if(can){
					double tmp=(hb[j]-hb[at]).ABS();
					if(ijk[j]>tmp+ijk[at]){
						ijk[j]=tmp+ijk[at];
						rev[j]=at;
					}
				}
			}
		}
	}
}