#include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#define INF 1000000000ll
#define MOD 1000000007ll
#define EPS 1e-8
#define REP(i, m) for(long long i=0; i<m; ++i)
#define FOR(i, n, m) for(long long i=n; i<m; ++i)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;

int main() {
	ios::sync_with_stdio(false);
	while(1) {
		map<vector<int>, int> d;
		vector<int> perm={0,1,2,3,4,5,6,7};
		do {
			d[perm]=INF;
		} while(next_permutation(ALL(perm)));
		vector<int> s(8);
		cin>>s[0];
		if(cin.eof()) break;
		FOR(i,1,8) cin>>s[i];
		queue<vector<int> > q;
		d[s]=0;
		q.push(s);
		vector<int> res={0,1,2,3,4,5,6,7};
		while(!q.empty()) {
			vector<int> p=q.front();
			if(p==res) break;
			q.pop();
			REP(i,8) {
				if(p[i]==0) {
					if(i>0&&i/4==(i-1)/4) {
						vector<int> np=p;
						swap(np[i],np[i-1]);
						if(d[np]==INF) {
							d[np]=d[p]+1;
							q.push(np);
						}
					}
					if(i<7&&i/4==(i+1)/4) {
						vector<int> np=p;
						swap(np[i],np[i+1]);
						if(d[np]==INF) {
							d[np]=d[p]+1;
							q.push(np);
						}
					}
					if(i+4<8) {
						vector<int> np=p;
						swap(np[i],np[i+4]);
						if(d[np]==INF) {
							d[np]=d[p]+1;
							q.push(np);
						}
					}
					if(i-4>=0) {
						vector<int> np=p;
						swap(np[i],np[i-4]);
						if(d[np]==INF) {
							d[np]=d[p]+1;
							q.push(np);
						}
					}
				}
			}
		}
		cout<<d[res]<<endl;
	}
}