#include<iostream>
#include<cstdio>
#include<fstream>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<cstring>
#define MAX 110000
#define INF 10000000

using namespace std;

map <string, int> dp;
int dir[4] = {1,-1,4,-4};

//BFS 
void Bfs(){
	//队列
	queue <string> que;
	que.push("01234567"); 
	dp["01234567"] = 0;
	while(!que.empty()){
		string now = que.front();
		que.pop();
		//找到0的位置
		int pos = 0;
		for(int i=0; i<8; i++){
			if(now[i] == '0'){
				pos = i;
				break;
			}
		} 
		//在0的位置四向遍历 
		for(int i=0; i<4; i++){
			int n = pos + dir[i];
			string next = now;
			if(n>=0 && n<8&&!(pos==3&&i==0)&&!(pos==4&&i==1)){
				swap(next[pos], next[n]);
				if(dp.find(next) == dp.end()){
					dp[next] = dp[now] + 1;	
					que.push(next);
				}			
			}
		}
	}
}

//测试函数 
int main()
{
    Bfs();
    string s;
    while(getline(cin,s)){
       s.erase(remove(s.begin(),s.end(),' '),s.end());
       cout<<dp[s]<<endl;
    }
    return 0;
}
