#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;

int direction[4] = { 1, -1, 4, -4 };
map<string, int>dp;

void bfs(){
	queue<string>q;
	q.push("01234567");
	while (!q.empty()){
		string cur = q.front();
		q.pop();
		int index = cur.find('0');
		for (int i = 0; i < 4; ++i){
			int n = index + direction[i];
			if (n >= 0 && n < 8 &&
				!(n == 3 && i == 0) &&
				!(n == 4 && i == 1))  {
				string next(cur);
				swap(next[index], next[n]);
				if (dp.find(next) == dp.end()){
					dp[next] = dp[cur] + 1;
					q.push(next);
				}
			}
		}
	}
}
int main()
{
	bfs();
	string start;
	while (getline(cin,start)){
		start.erase(remove(start.begin(), start.end(), ' '), start.end());
		cout << dp[start] << "\n";
	}
	return 0;
}