#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <cmath>
using namespace std;
struct P{string a;int t,xh;}S,z,lins;
int op[]={1,4,-1,-4};
map <string,int> dp;
bool qianj(P &zt,int f)
{
	int i,xh=zt.xh+op[f],sum=0;
	if (xh<0||xh>7||(zt.xh==3&&op[f]==1)||(zt.xh==4&&op[f]==-1)) return 0;
	swap(zt.a[zt.xh],zt.a[xh]);
	for (i=0;i<8;i++) cout<<zt.a;
	cout<<endl;
	map <string,int>::iterator it=dp.find(zt.a);
	if (it != dp.end()) return 0;//dp.count(zt.a)!=0
	zt.xh=xh;
	zt.t++;
	dp[zt.a]=zt.t;
	return 1;
}
void bfs(){
	queue <P> que;
	que.push(S);
	while (!que.empty()){
		for (int i=0;i<4;i++){
			lins=que.front();
			if (qianj(lins,i))
				que.push(lins);
		}
		que.pop();
	}
}
int main()
{
	int i,j;
	double s=0;
	z.a="01234567",z.xh=0,z.t=0;
	bfs();
	printf("%d\n",dp.size());
	while (~scanf("%d",&S.a[0]))
	{
		for (i=1;i<8;i++)
			scanf("%d",&S.a[i]);
		printf("%d\n",dp[S.a]);
	}
	return 0;
}