#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
const int inf = 0x7f7f7f;
//const int maxn = ;
int dx[] = {-1, 1, -4, 4};
map<string, int> m;

void prework() {
	queue<string>que;
	string temp, now;
	temp = "01234567";
	que.push(temp);
	m[temp] = 1;
	while(!que.empty()) {
		now = que.front();
		que.pop();
		int pos = 0;
		for(int i = 0; i < 8; i++)
			if(now[i] == '0')
				pos = i;
		for(int i = 0; i < 4; i++) {
			int x = pos + dx[i];
			//if(x < 0 || x >= 8 || (x == 3 && dx[i] == 1) || (x == 4 && dx[i] == -1)) continue;
			if(0 <= x && x < 8 && !(pos == 3 && dx[i] == 1) && !((pos == 4 && dx[i] == -1))) {
				temp = now;
				swap(temp[pos], temp[x]);
				if(m[temp] == 0) {
					que.push(temp);
					m[temp] = m[now] + 1;
				}
			}
		}
	}
}
int main() {
//ios::sync_with_stdio(false);
#ifdef LOCAL
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
#endif
	prework();
	while(true) {
		string s;
		int in;
		for(int i = 0; i < 8; i++) {
			if(!(cin >> in)) return 0;
			s += (in + '0');
		}
		cout << m[s] - 1 << endl;
	}
}


