#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;

const int dy[] = {0, -1, 0, 1};
const int dx[] = {-1, 0, 1, 0};
map<int, int> table;

void enumerate(int puzzle[], int depth)
{
	int num = 0;
	pii zero;
	rep(i, 8){
		num = num*8 + puzzle[i];
		if(puzzle[i] == 0)
			zero = pii(i/4, i%4);
	}
	
	if(table.count(num))
		return;
	table[num] = depth;
	
	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(puzzle[zero.first*4+zero.second], puzzle[py*4+px]);
		enumerate(puzzle, depth+1);
		swap(puzzle[zero.first*4+zero.second], puzzle[py*4+px]);
	}
}

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