#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
#include <sstream>
#include <map>
#include <tuple>

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mt(a,b,c) make_tuple(a,b,c)
#define all(a) a.begin(),a.end()
#define pii pair<int, int>
#define ll long long
using namespace std; 

#define MAXT 1000
#define MAXM 50000
#define SIZE 302
#define INF 1<<30
typedef tuple<string, int, int> T;
typedef queue<tuple<string, int, int> > Q;

Q que;
int pz[2][4];
map<string, int> pz_ans;

void bfs()
{
	string s = "01234567";
	pz_ans.insert(mp(s,1));
	T tp = mt(s,1,0);
	que.push(tp);
	int cnt, pos;

	while(!que.empty()) {
		tp = que.front(); que.pop();
		s = get<0>(tp);
		cnt = get<1>(tp);
		pos = get<2>(tp);
		rep(i,0,2) rep(j,0,4) pz[i][j] = s[i*4+j];
		int y = pos/4;
		int x = pos%4;

		if (pos%4) { // left swap
			swap(pz[y][x], pz[y][x-1]);
			rep(i,0,8) s[i] = char(pz[i/4][i%4]);
			if (pz_ans[s] == 0) {
				pz_ans[s] = cnt;
				tp = mt(s,cnt+1,pos-1);
				que.push(tp);
			}
			swap(pz[y][x], pz[y][x-1]);
		}

		if (pos%4 < 3) { // right swap
			swap(pz[y][x], pz[y][x+1]);
			rep(i,0,8) s[i] = char(pz[i/4][i%4]);
			if (pz_ans[s] == 0) {
				pz_ans[s] = cnt;
				tp = mt(s,cnt+1,pos+1);
				que.push(tp);
			}
			swap(pz[y][x], pz[y][x+1]);
		}

		if (pos/4) { // up swap
			swap(pz[1][x], pz[0][x]);
			rep(i,0,8) s[i] = char(pz[i/4][i%4]);
			if (pz_ans[s] == 0) {
				pz_ans[s] = cnt;
				tp = mt(s,cnt+1,pos-4);
				que.push(tp);
			}
			swap(pz[1][x], pz[0][x]);
		}

		if (pos/4 == 0) { // down swap
			swap(pz[0][x], pz[1][x]);
			rep(i,0,8) s[i] = char(pz[i/4][i%4]);
			if (pz_ans[s] == 0) {
				pz_ans[s] = cnt;
				tp = mt(s,cnt+1,pos+4);
				que.push(tp);
			}
			swap(pz[0][x], pz[1][x]);
		}
	}
	s = "01234567";
	pz_ans.erase(s);
	pz_ans[s] = 0;
}

void input()
{
	string str;
	while(getline(cin,str)) {
		string::iterator end_it = remove(str.begin(), str.end(), ' ');
		str.erase(end_it, str.end());
		cout << pz_ans[str] << endl;
	}
}

int main(void)
{
	bfs();
	input();
	return 0;
}