#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <cstdlib>

#define SIZE 8
#define WIDTH 4

using namespace std;

map<vector<int>, int> distances;
int way[4] = { -1, 1, -WIDTH, WIDTH };

void solve(){
	vector<int> start(SIZE);
	queue<vector<int>> q;

	for (int i = 0; i < SIZE; i++)
		start[i] = i;
	q.push(start);
	distances[start] = 0;

	while (!q.empty()){
		vector<int> field = q.front();
		q.pop();

		for (int i = 0; i < SIZE; i++){
			if (field[i] == 0){
				for (int j = 0; j < 4; j++){
					int newpos = i + way[j];
					if (newpos < 0 || newpos >= SIZE || abs(newpos%WIDTH - i%WIDTH) > 1) continue;
					int dist = distances[field];
					field[i]		= field[newpos];
					field[newpos]	= 0;
					
					if (distances.count(field)){
						field[newpos] = field[i];
						field[i] = 0;
						continue;
					};

					distances[field] = dist + 1;
					q.push(field);

					field[newpos] = field[i];
					field[i] = 0;
				}
				break;
			}
		}
	}
}



int main(){
	solve();
	
	
	while (!cin.eof()){
		vector<int> input(SIZE);
		for (int i = 0; i < SIZE; i++)
			cin >> input[i];
		cout << distances[input] << endl;
	}
}