#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>


#ifdef _DEBUG
#define typeof(X) std::identity<decltype(X)>::type //C++0x (for vs2010)
#else
#define typeof(X) __typeof__(X) // for gcc
#endif

#define sz(a)  int((a).size())
#define FOREACH(it, c) for (typeof((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define FOR(i,count) for (int i = 0; i < (int)(count); i++)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
const int MODULO = 100000 ;
const int INF = 100000000; //1e8

typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef complex<double> Cd;

using namespace std;

int tbl[8];

int encode(int x[8]){
	int p[8];
	int ret = 0;
	FOR(i,8) p[i] = i;
	FOR(i,8){
		int a = x[i];
		ret += p[a] * tbl[i];
		for(int i = a+1; i < 8; i++) p[i]--;
	}
	return ret;
}

void decode(int x[8],int val){
	int p[8];
	FOR(i,8) p[i] = i;
	FOR(i,7){
		int a = val / tbl[i];
		val -= a * tbl[i];
		x[i] = p[a];
		for(int i = a; i < 7; i++) p[i] = p[i+1];
	}
	x[7] = p[0];
}

bool b[8*7*6*5*4*3*2*1];

bool solve(){

	memset(b,0,sizeof(b));
	int start[8]; FOR(i,8) 
		if(!(cin>>start[i])) 
			return false;

	queue<Pii> q;
	int de = encode(start);
	if(de == 0){
		puts("0");
		return true;
	}

	q.push(Pii(de,0));
	b[de] = true;
	while(!q.empty()){
		int x[8];
		Pii pii = q.front(); q.pop();
		decode(x,pii.first);
		int cnt = pii.second;
		int zero_id = find(x,x+8,0) - x;
		static int dir[4] = {-4,-1,1,4};
		FOR(j,4){
			int n_zero = zero_id + dir[j];
			if(n_zero < 0 || n_zero >= 8 || n_zero + zero_id == 7) continue;
			swap(x[n_zero],x[zero_id]);
			int n_de = encode(x);
			if(!b[n_de]){
				if(n_de == 0){
					printf("%d\n",cnt + 1);
					return true;
				}
				b[n_de] = true;
				q.push(Pii(n_de,cnt + 1));
			}
			swap(x[n_zero],x[zero_id]);
		}
	}
	
	assert(0);
}

int main()
{
	tbl[6] = 1;
	for(int i = 5; i >= 0; i--) tbl[i] = tbl[i+1] * (7-i);

	while(true){
		if(!solve())
			break;
	}

	return 0;
}