#include<bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i = 0; i < (a); i++)
#define reps(i, a, b) for(int i = (a); i < (b); i++)
#define INF 1e9
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
class Data {
	public:
	vii status;
	pii zero;
	int cnt;
	Data(){}
	Data(vii status, pii zero,int cnt) : status(status), zero(zero), cnt(cnt){}
};
string to_s(vector<vi> s) {
	string ret;
	for(auto it : s) {
		for(auto i : it) {
			ret += '0' + i;
		}
	}
	return ret;
}
int main() {
	vii ans = {{0,1,2,3},{4,5,6,7}};
	vii v = vii(2, vi(4, 0));
	while(1) {
		cin >> v[0][0];
		if(cin.eof()) {
			break;
		}
		int zx = 0,zy = 0;
		for(int i = 1; i < 8; i++) {
			cin >> v[i/4][i%4];
			if(v[i/4][i%4] == 0) {
				zy = i / 4;
				zx = i % 4;
			}
		}
		map<string, int> memo;
		queue<Data> Q;
		Q.push(Data(v,make_pair(zx, zy), 0));
		while(!Q.empty()) {
			auto d = Q.front();
			Q.pop();
			if(d.status == ans) {
				printf("%d\n", d.cnt);
				break;
			}
			string tmp = to_s(d.status);
			if(memo[tmp]) continue;
			memo[tmp] = 1;
			auto z = d.zero;
			rep(i, 4) {
				int nx = dx[i] + z.first, ny = dy[i] + z.second;
				if(nx < 0 || nx >= 4 || ny < 0 || ny >= 2) continue;
				auto p = d.status;
				swap(p[z.second][z.first], p[ny][nx]);
				Q.push(Data(p,make_pair(nx,ny), d.cnt + 1));
			}
		}
	}
	return 0;
}