#include <stdio.h>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <map>

#define rep(i, n) for(int i=0; i<(int)(n); i++)
using namespace std;

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

bool is_answer(const vector<int>& v) {
	rep(i, 8) if(v[i]!=i) return false;
	return true;
}

int main()
{
	int buf[8];
	while(scanf("%d", buf)!=EOF) {
		rep(i, 7) scanf("%d", buf+i+1);
		vector<int> v;
		rep(i, 8) v.push_back(buf[i]);
		queue<pair<int, vector<int> > > q;
		q.push(make_pair(0, v));
		set<vector<int> > visited;
		while(!q.empty()) {
			int vc = q.front().first;
			vector<int> vv = q.front().second;
			q.pop();
			if(visited.find(vv)!=visited.end()) continue;
			visited.insert(vv);
			if(is_answer(vv)) {
				printf("%d\n", vc);
				break;
			}
			int lz=-1;
			rep(i, 8) if(vv[i]==0) { lz=i; break; }
			rep(d, 4) {
				if(0<=lz+dd[d] && lz+dd[d]<8
					&& !(lz==4 && dd[d]==1 || lz==5 && dd[d]==-1)) {
					vector<int> nvv(vv);
					swap(nvv[lz+dd[d]], nvv[lz]);
					if(visited.find(nvv)!=visited.end()) continue;
					q.push(make_pair(vc+1, nvv));
				}
			}
		}
	}
}