#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>
#include <ctime>
 
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;
const ll MOD = 1e9 + 7;
double EPS = 1e-7;

vi d(8, 0), t;
vi a(8, 0);

int b;

set<int> s;
pair<int ,int> p;

int ans = 0;
int c;
int tt;

bool check(vi v){
	for(int i = 0; i < 8; i++){
		if(v[i]!=i) return false; 
	}
	return true;
}

int calc1(const vi& v){
	int ret = 0, p = 1;
	for(int i = 0; i < v.size(); i++){
		ret += p * v[i];
		p *= 8;
	}
	return ret;
}
vi calc2(int p){
	vi ret(8, 0);
	int idx = 0;
	while(p > 0 && idx < 8){
		ret[idx] = p%8;
		p /= 8;
		idx++;
	}
	return ret;
}

int main(){
	rep(i, 8) a[i]=i;
	b = calc1(a);
	int cnt = 0;
	while(/*cin>>d[0]*/scanf("%d", &d[0])!=EOF){
		//rep(i, 7) cin>>d[i+1];
		rep(i, 7){
			scanf("%d", &d[i+1]);
		}
		s.clear();
		queue<pair<int ,int> > q;
		q.push(mp(0, calc1(d)));
		s.insert(calc1(d));
		cnt = 0;
		while(!q.empty()){
		//cout<<cnt++<<endl;
			cnt++;
			p = q.front(); q.pop();
			c = p.fi;
			d = t = calc2(p.se);
			tt = calc1(t);
			// if(cnt == 0){
			// 	for(int i = 0; i < 8; i++){
			// 		cout<<" "<<t[i];
			// 	}
			// 	cout<<endl;
			// }
			if(/*check(t)*/tt == b){
				//cout<<c<<endl;
				printf("%d\n", c);
				break;
			}
			for(int i = 0; i < 8; i++){
				//cout<<" "<<i<<endl;
				if(d[i] == 0){
					if(i%4!=0){
						t = d;
						swap(t[i], t[i-1]);
						tt = calc1(t);
						if(s.count(tt)==0){
							q.push(mp(c+1, tt));
							s.insert(tt);
						}
					}
					if(i%4!=3){
						t = d;
						swap(t[i], t[i+1]);
						tt = calc1(t);
						if(s.count(tt)==0){
							q.push(mp(c+1, tt));
							s.insert(tt);
						}
					}
					if(i/4==0){
						t = d;
						swap(t[i], t[i+4]);
						tt = calc1(t);
						if(s.count(calc1(t))==0){
							q.push(mp(c+1, tt));
							s.insert(tt);
						}
					}
					else{
						t = d;
						swap(t[i], t[i-4]);
						tt = calc1(t);
						if(s.count(tt)==0){
							q.push(mp(c+1, tt));
							s.insert(tt);
						}
					}
					break;
				}
			}
		}
	}
	return 0;
}