#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
#include <cmath>
#include <climits>
#include <set>
#include <iostream>
#include <map>
#include <functional>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <complex>
#include <sstream>
#include <stack>

using namespace std;

#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef complex<double> Point;

int solve(int puzzle[])
{
	set<int> visited;
	
	int num = 0;
	for(int i=7; i>=0; --i)
		num = num*10 + puzzle[i];
	queue<pii> Q;
	Q.push(pii(num, 0));
	
	int ans;
	while(!Q.empty()){
		pii p = Q.front();
		Q.pop();
		if(visited.count(p.first))
			continue;
		visited.insert(p.first);
		
		if(p.first == 76543210){
			ans = p.second;
			break;
		}
		
		pii zero;
		int puz[8];
		rep(i, 8){
			puz[i] = p.first % 10;
			p.first /= 10;
			if(puz[i] == 0)
				zero = pii(i/4, i%4);
		}
		
		const int dy[] = {-1, 0, 0, 1};
		const int dx[] = {0, -1, 1, 0};
		rep(i, 4){
			int py = zero.first + dy[i];
			int px = zero.second + dx[i];
			if(py<0 || 2<=py || px<0 || 4<=px)
				continue;
			swap(puz[zero.first*4+zero.second], puz[py*4+px]);
			int num = 0;
			for(int j=7; j>=0; --j)
				num = num*10 + puz[j];
			Q.push(pii(num, p.second+1));
			swap(puz[zero.first*4+zero.second], puz[py*4+px]);
		}
	}
	
	return ans;
}

int main()
{
	int puzzle[8];
	for(;;){
		rep(i, 8){
			if(scanf("%d", &puzzle[i]) == EOF)
				return 0;
		}
		printf("%d\n", solve(puzzle));
	}
}