#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
typedef pair< vector< vector<int > >, int >  NODE;
vector< vector<int> >data(4,vector<int>(6,-1)),goal;
int dx[]={ 1,-1, 0, 0};
int dy[]={ 0, 0,-1, 1};
int main(){
	bool flag = false;
	goal = data;
	for(int i=0;i<2;i++)
		for(int j=0;j<4;j++)
			goal[1+i][1+j] = j+i*4;
	while(1){
		for(int i=1;i<=2;i++)
			for(int j=1;j<=4;j++)
				if(cin >> data[i][j]);
				else return 0;
		
		map< vector< vector<int > >	, bool > done; 
		queue<NODE> Q;
		vector< vector<int> >now;
		int depth;
		Q.push(make_pair(data,0));
	
		while(!Q.empty()){
			now = Q.front().first;
			depth = Q.front().second;
			Q.pop();
			if(now == goal){
				cout << depth << endl;
				break;
			}
			done[now] = true;
		
			for(int i=1;i<=2;i++)
				for(int j=1;j<=4;j++)
					if(now[i][j]==0){
						for(int P=0;P<4;P++){
							if(now[i+dy[P]][j+dx[P]] != -1){
								swap(now[i+dy[P]][j+dx[P]],now[i][j]);
								if(!done[now])Q.push(make_pair(now,depth+1));
								swap(now[i+dy[P]][j+dx[P]],now[i][j]);
							}
						}
					}
		}
	}
}