#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;

int i,j,k;
map<vector<vector<int> > , int> memo;

bool is_goal(vector<vector<int> > inans){
	vector<vector<int> > ans(2, vector<int>(4));	//テ・ツ按敕ヲツ慊淌・ツ個姪・ツソツ?ゥツ??
	for(i=0; i<8; i++){
			int x = i % 4;
			int y = i / 4;
			ans[y][x] = i;
	}
	//cout << "goalテ・ツョツ淌ィツ。ツ? <<endl;
	if(inans == ans)	return true;
	return false;
}
/*
void checker(vector<vector<int>> check, string str, int i){
	cout << str << " , " << i << endl;
	for(i=0; i<8; i++){
			int x = i % 4;
			int y = i / 4;
			cout << "check[" << y << "][" << x << "]=" << check[y][x] <<endl;
	}
	cout << " " << endl;
}
*/

int BFS(vector<vector<int> > inans){

	memo[inans] = 0;
	queue<vector<vector<int> > > q;
	q.push(inans);

	while( !q.empty() ){
		vector<vector<int> > now = q.front();
		q.pop();
	//					checker(now,"now",500);
	//	if(is_goal(now) == true)	return memo[now];

		int nowcost = memo[now];
	//					cout << "nowcost = " <<nowcost <<endl;

		//vector<vector<int>> next = now;

		for(i=0; i<8; i++){
			int x = i % 4;
			int y = i / 4;
			//left
			if(x != 0 && now[y][x]==0){
				swap(now[y][x-1] , now[y][x]);
				while( !memo.count(now) ){
					memo[now] = nowcost + 1;
					q.push(now);
				}
	//			checker(now,"left",i);
				swap(now[y][x-1] , now[y][x]);
			}
			//down
			if(y != 1 && now[y][x]==0){
				swap(now[y+1][x] , now[y][x]);
				while( !memo.count(now) ){
					memo[now] = nowcost + 1;
					q.push(now);
				}
	//			checker(now,"down",i);
				swap(now[y+1][x] , now[y][x]);
			}
			//right
			if(x != 3 && now[y][x]==0){
				swap(now[y][x+1] , now[y][x]);
				while( !memo.count(now) ){
					memo[now] = nowcost + 1;
					q.push(now);
				}
	//			checker(now,"right",i);
				swap(now[y][x+1] , now[y][x]);
			}
			//up
			if(y != 0 && now[y][x]==0){
				swap(now[y-1][x] , now[y][x]);
				while( !memo.count(now) ){
					memo[now] = nowcost + 1;
					q.push(now);
				}
	//			checker(now,"up",i);
				swap(now[y-1][x] , now[y][x]);
			}
		} //cout << q.size() <<endl;
	}
	return -1;
}



int main()
{
	    vector<vector<int> > a(2, vector<int>(4)); 
        for(i = 0; i < 8; i++){ 
            int xx = i % 4, yy = i / 4; 
            a[yy][xx] = i; 
        } 
        // テ・ツケツ?・ツ?ェテ・ツ?暗ヲツ篠「テァツエツ「 (テヲツ慊?・ツ按敕」ツ?ォテ」ツ?凖」ツ?ケテ」ツ?ヲテ」ツ?ョティツァツ」テ」ツつ津ヲツアツづ」ツつ?」ツ?ヲテ」ツ?甘」ツ?? 
        int ans = BFS(a); 
		
		


	vector<vector<int> > in(2, vector<int>(4));	//テ・ツ按敕ヲツ慊淌・ツ個姪」ツ?療」ツ?ェテ」ツ??」ツ?ィテ」ツつィテ」ツδゥテ」ツδシテ・ツ?コテ」ツつ?
	while(cin >> in[0][0]){
		for(i=1; i<8; i++){
			int x = i % 4;
			int y = i / 4;
			cin >> in[y][x];
		}
	//	checker(in,"first",100);
		cout << memo[in] << endl;
	}
}