//Problem:
//Date:
//Skill:

#define CLR(a) memset((a),0,sizeof(a))
#define MAX 2140000000;
#define RE(i,n)  for(i=0;i<n;i++)
#define RE2(i,n) for(i=1;i<=n;i++)
#include<algorithm>
#include<iostream>
#include<time.h>
#include<string.h>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int shift[4] = { -4,-1,1,4 };
class node {
public:
	int paces;
	int pos;
	string arrange;
};
inline bool change(int i, node &sour)
{
	if (sour.pos < 4 && shift[i] == -4)return 0;
	if (sour.pos >= 4 && shift[i] == 4)return 0;
	if (sour.pos % 4 == 3 && shift[i] == 1)return 0;
	if (sour.pos % 4 == 0 && shift[i] == -1)return 0;

	int temp = sour.pos + shift[i];
//	if (temp < 0 || temp >= 8)return false;
	{
		swap(sour.arrange[sour.pos], sour.arrange[temp]);
	}

	
	sour.pos = temp;
	sour.paces++;
	return 1;
}

int bfs(node end,node star)
{
	map<string,int>ma;
	queue<node>Q;
	node store;
	Q.push(end);
//
	store = Q.front();
	if (store.arrange == star.arrange)return store.paces;
	ma[store.arrange] = store.paces;



//	ma[end.arrange] = end.paces;
	while (!Q.empty())
	{
		for (int i = 0; i < 4; i++)
		{
			store = Q.front();
			if (change(i, store) == 0)continue;//越界
			//是否重复
			if (ma.find(store.arrange)!=ma.end())continue;//重复
			
			if (store.arrange == star.arrange)return store.paces;

			ma[store.arrange] = store.paces;
			Q.push(store);

		}
		Q.pop();
	}

	return -1;
}

//#define LOCAL
int main()
{
#ifdef LOCAL

	freopen("C:\\Users\\VULCAN\\Desktop\\data.in", "r", stdin);
	//	freopen("C:\\Users\\VULCAN\\Desktop\\data.out", "w", stdout);
#endif
	//这是一条帅气的分界线，写程序从这里开始吧
	const node end = { 0,0,"01234567"};
	node star;
	while (getline(cin,star.arrange))
	{
		for (int i = 1; i < 8; i++)
		{			if (star.arrange[i] == 0)star.pos = i;		}
	//	star.arrange.erase(' ');
		star.arrange.erase(remove(star.arrange.begin(), star.arrange.end(), ' '), star.arrange.end());
		star.paces = 0;

		cout << bfs(end, star) << endl;
	}
		







	//这是一条可爱的分界线
#ifdef LOCAL
	printf("用时%.3f秒\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}





