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

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

map<string,int> mat;

int main()
{
	string s;
	mat["01234567"] = 0;
	queue<string> q;
	q.push(string("01234567"));
	while(!q.empty()){
		s = q.front();q.pop();
		int x = s.find("0");
		for(int i = 0; i < 4; ++i){
			int nx = x + dx[i];
			if(0 <= nx && nx < 8){
				if(x == 3 && nx == 4 || x == 4 && nx == 3)continue;
				string t = s;
				swap(t[x],t[nx]);
				if(mat.find(t) != mat.end())continue;
				q.push(t);mat[t] = mat[s] + 1;
			}
		}
	}
	
	while(getline(cin,s)){
		s.erase(remove(s.begin(),s.end(),' '),s.end());
		cout << mat[s] << endl;
	}
}