#include <cstdio>
#include <queue>
#include <vector>
#include <utility>
#include <map>

using namespace std;

const int dy[4] = {-1,0,1,0}, dx[4] = {0,1,0,-1};


int main(void){
	int t[2][4], i, j, k, f = 1, val, num, x, y;
	queue<pair<pair<vector<vector<int> >,int>,pair<int,int> > > que; // array , count, present location
	map<int,bool> mib;
	while(1){
		vector<vector<int> > vvi, vvit;
		for(i = 0;i < 2 && f != EOF;i++){
			vector<int> vi;
			for(j = 0;j < 4 && f != EOF;j++){
				f = scanf("%d",&val);
				vi.push_back(val);
			}
			vvi.push_back(vi);
		}
		if(f == EOF){
			break;
		}
		for(i = 0;i < 2;i++){
			for(j = 0;j < 4;j++){
				if(vvi[i][j] == 0){
					break;
				}
			}
			if(j != 4){
				break;
			}
		}
		que.push(make_pair(make_pair(vvi,0),make_pair(i,j)));
		while(que.size()){
			vvi = que.front().first.first;
			num = que.front().first.second;
			y = que.front().second.first;
			x = que.front().second.second;
			que.pop();
			val = 0;
			for(i = 0;i < vvi.size();i++){
				for(j = 0;j < vvi[i].size();j++){
					val = val * 10 + vvi[i][j];
				}
			}
			if(mib.size() != 0 && mib.find(val) != mib.end()){
				continue;
			}
			mib.insert(map<int,bool>::value_type(val,true));
			for(i = 0;i < 7;i++){
				if(vvi[i / 4][i % 4] > vvi[(i + 1) / 4][(i + 1) % 4]){
					break;
				}
			}
			if(i == 7){
				break;
			}
			for(k = 0;k < 4;k++){
				if(0 <= y + dy[k] && y + dy[k] < 2 && 0 <= x + dx[k] && x + dx[k] < 4){
					vvit = vvi;
					swap(vvit[y][x],vvit[y + dy[k]][x + dx[k]]);
					que.push(make_pair(make_pair(vvit,num + 1),make_pair(y + dy[k],x + dx[k])));
				}
			}
		}
		printf("%d\n",num);
		while(que.size()){
			que.pop();
		}
		mib.clear();
	}
	return 0;
}