#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<cctype>
#include<map>
#include<iostream>
#define INF 100000000
using namespace std;

/*
map<int,string>,用数?方式插入数据：map[1]="abshh";
map<string,int> map["asido"]=1;
*/

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

void bfs()
{
	queue<string> q;
	string s = "01234567";
	q.push(s);
	string now, next;
	while (!q.empty())
	{
		now = q.front(); q.pop();
		int p = 0;
		for (int i = 0;i < 8;i++)
		{
			if (now[i] == '0') {
				p = i;break;
			}
		}
		for (int i = 0;i < 4;i++)
		{
			int nt = p + d[i];
			if (nt >= 0 && nt < 8 && !(i == 0 && p == 3) && !(i == 1 && p == 4))
			{
				next = now;
				swap(next[p], next[nt]);
				//if(dp.find(next)==dp.end())//
				if (dp[next] == 0)
				{
					q.push(next);
					dp[next] = dp[now] + 1;
				}
			}
		}
	}
}


int main()
{
	dp["01234567"] = 0;
	bfs();
	while (1)
	{
		string aa;
		for (int i = 0;i < 8;i++)
		{
			int c;
			scanf("%d", &c);
			aa += c + '0';
		}
		printf("%d\n", dp[aa]);
	}
	return 0;
}