#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;

int base[8];
string target="01234567";
map<string,int> dist;
void exec()
{
	string v=target;
	for(int i=0;i<8;i++){
		if (cin >> v[i]){
		}
		else
		{
			exit(0);
		}
	}
	cout << dist[v] << endl;
}
int main() {
	base[0]=1;
	for(int i=1;i<8;i++){
		base[i]=10*base[i-1];
	}
	dist[target]=0;
	int d=1;
	vector<string> cur;
	vector<string> nex;
	cur.push_back(target);
	while(cur.size()){
		for(int i=0;i<cur.size();i++){
			for(int r=0;r<2;r++){
				for(int c=0;c<4;c++){
					if(cur[i][r*4+c]=='0'){
						if(0<c){
							string v=cur[i];
							swap(v[r*4+c],v[r*4+c-1]);
							if(dist.find(v)==dist.end()){
								dist[v]=d;
								nex.push_back(v);
							}
						}
						if(c<3){
							string v=cur[i];
							swap(v[r*4+c],v[r*4+c+1]);
							if(dist.find(v)==dist.end()){
								dist[v]=d;
								nex.push_back(v);
							}
						}
						{
							int rr=(r+1)%2;
							string v=cur[i];
							swap(v[r*4+c],v[rr*4+c]);
							if(dist.find(v)==dist.end()){
								dist[v]=d;
								nex.push_back(v);
							}
						}
					}
				}
			}
		}
		cur.swap(nex);
		nex.clear();
		d++;
	}
	while(1){
		exec();
	}
}