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

typedef map <string,int> M;
M puzzle;

int main(void){
	string S="01234567";
	puzzle[S]=0;
	queue<string> q;
	q.push(S);
	while(!q.empty()){
  		string tmp=q.front();
		q.pop();
		int num=puzzle[tmp];
		int zero;
		for(int i=0;i<8;i++)
			if(tmp[i]=='0')
				zero=i;
		if(0<=zero && zero<=3){
			swap(tmp[zero],tmp[zero+4]);
			if(puzzle.find(tmp) == puzzle.end())
				puzzle[tmp]=(num+1),q.push(tmp);
			swap(tmp[zero],tmp[zero+4]);
		}
		if(4<=zero && zero<=7){
			swap(tmp[zero],tmp[zero-4]);
			if(puzzle.find(tmp) == puzzle.end())
				puzzle[tmp]=(num+1),q.push(tmp);
			swap(tmp[zero],tmp[zero-4]);
		}
		if(zero%4 !=0){
			swap(tmp[zero],tmp[zero-1]);
			if(puzzle.find(tmp) == puzzle.end())
				puzzle[tmp]=(num+1),q.push(tmp);
			swap(tmp[zero],tmp[zero-1]);
		}
		if(zero%4 !=3){
			swap(tmp[zero],tmp[zero+1]);
			if(puzzle.find(tmp) == puzzle.end())
				puzzle[tmp]=(num+1),q.push(tmp);
			swap(tmp[zero],tmp[zero+1]);
		}
	}
	while(1){
		string tar,tmp;
		for(int i=0;i<8;i++)
			cin >> tmp,tar+=tmp;
		cout << tar << endl;
		if(cin.eof())
			break;
		int ans=puzzle[tar];
		cout << ans << endl;
	}
	return 0;
}