#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
#include <ctime>
#include <cstring>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;


int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool f[8][8][8][8][8][8][8][8];
int r[2][4]={{0,1,2,3},{4,5,6,7}};
struct puzzle{
	int card[2][4];
	int cnt;
};

int main(){
	int n[2][4];
	while(cin>>n[0][0]>>n[0][1]>>n[0][2]>>n[0][3]>>n[1][0]>>n[1][1]>>n[1][2]>>n[1][3]){
		int ans=0;
		puzzle sp;
		for(int i=0; i<2; ++i){
			for(int j=0; j<4; ++j){
				sp.card[i][j]=n[i][j];
			}
		}
		clock_t c1,c2,c3,c4;
		c1 = clock();
		/*
		for(int i=0;i<8;++i)for(int j=0;j<8;++j)for(int k=0;k<8;++k)for(int l=0;l<8;++l)for(int m=0;m<8;++m)for(int n=0;n<8;++n)for(int o=0;o<8;++o)for(int p=0;p<8;++p) f[i][j][k][l][m][n][o][p]=false;
		*/
		memset(f,false,sizeof(f));
		c2=clock();
		sp.cnt=0;
		queue<puzzle> que;
		que.push(sp);
		c3 = clock();
		while(que.size()){
			puzzle pz=que.front();
			que.pop();
			f[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]][pz.card[1][0]][pz.card[1][1]][pz.card[1][2]][pz.card[1][3]]=true;
			bool flag=true;
			for(int i=0; i<2; ++i){
				for(int j=0; j<4; ++j){
					if(pz.card[i][j]!=r[i][j]){
						flag=false;
						break;
					}
				}
			}
			if(flag){
				ans=pz.cnt;
				break;
			}
			for(int i=0; i<2; ++i){
				for(int j=0; j<4; ++j){
					if(pz.card[i][j]==0){
						if(0<=i-1){
							puzzle np=pz;
							np.cnt=pz.cnt+1;
							swap(np.card[i-1][j],np.card[i][j]);
							if(!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]][np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])que.push(np);
						}
						if(i+1<2){
							puzzle np=pz;
							np.cnt=pz.cnt+1;
							swap(np.card[i+1][j],np.card[i][j]);
							if(!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]][np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])que.push(np);
						}
						if(0<=j-1){
							puzzle np=pz;
							np.cnt=pz.cnt+1;
							swap(np.card[i][j-1],np.card[i][j]);
							if(!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]][np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])que.push(np);
						}
						if(j+1<4){
							puzzle np=pz;
							np.cnt=pz.cnt+1;
							swap(np.card[i][j+1],np.card[i][j]);
							if(!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]][np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])que.push(np);
						}
					}
				}
			}
		}
		c4 = clock();
		cout << ans << "\n";
		cout << (double)(c2-c1) << "\n";
		cout << (double)(c4-c3) << "\n";
	}
}