#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
#define DBG(X) cout<<#X<<" : "<<X<<endl;
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

int ans = INF;

set<vi> st;

void calc(vi v, int p, int n){
	if(n > ans || st.count(v)>0) return;
	rep(i, 8) {
		if(v[i]!=i) break;
		if(i==7) {
			ans = min(ans, n);
			return;
		}
	}
	st.insert(v);

	if(p!=0&&p!=4){
		swap(v[p],v[p-1]);
		calc(v,p-1, n+1);
		swap(v[p],v[p-1]);
	}
	if(p!=3&&p!=7){
		swap(v[p],v[p+1]);
		calc(v,p+1, n+1);
		swap(v[p],v[p+1]);
	}
	if(p<4){
		swap(v[p],v[p+4]);
		calc(v,p+4, n+1);
		swap(v[p],v[p+4]);
	}
	else{
		swap(v[p],v[p-4]);
		calc(v,p-4, n+1);
		swap(v[p],v[p-4]);
	}
}

int main(){
	vi v(8); int p;
	while(cin>>v[0]){
		p = 0;
		for(int i = 1; i < 8; i++){
			cin>>v[i];
			if(v[i]==0) p = i;
		}
		ans = INF;
		st.clear();
		calc(v, p, 0);
		cout<<ans<<endl;
	}
}