#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX 8

using namespace std;

struct Board{
	int num[MAX];
	int pos[MAX];
	int prev;
	int depth;
	void print() const {
		cout << num[0];
		for(int i = 1; i < MAX; ++i)
			cout << ' ' << num[i];
		cout << '\n';
	}
	bool check() const {
		for(int i = 0; i < MAX; ++i)
			if( num[i] != i )
				return false;
		return true;
	}
	bool operator==(const Board &b) const {
		for(int i = 0; i < MAX; ++i)
			if( num[i] != b.num[i] )
				return false;
		return true;
	}
} ;

inline void SubNext(Board &bd, int from, int to){
	swap( bd.pos[bd.num[from]], bd.pos[bd.num[to]] );
	swap( bd.num[from], bd.num[to] );
	bd.prev = from;
}
void Next( const Board &b, vector<Board> &vNext ){
	Board bt[3];
	int p, x;

	for(int i = 0; i < 3; ++i){
		bt[i] = b;
		bt[i].prev = -1;
		bt[i].depth = b.depth + 1;
	}
	x = b.pos[0];
	p = b.prev;

	switch( x ){
		case 0:
			switch( p ){
				case -1: SubNext(bt[0], x, 1); SubNext(bt[1], x, 4); break;
				case 1:  SubNext(bt[0], x, 4); break;
				case 4:  /*SubNext(bt[0], x, 1);*/ break;
			}
			break;
		case 1:
			switch( p ){
				case -1: SubNext(bt[0], x, 0); SubNext(bt[1], x, 2); SubNext(bt[2], x, 5); break;
				case 0:  /*SubNext(bt[0], x, 2); SubNext(bt[1], x, 5);*/ break;
				case 2:  SubNext(bt[0], x, 0); /*SubNext(bt[1], x, 5);*/ break;
				case 5:  SubNext(bt[0], x, 0); /*SubNext(bt[1], x, 2);*/ break;
			}
			break;
		case 2:
			switch( p ){
				case -1: SubNext(bt[0], x, 1); SubNext(bt[1], x, 3); SubNext(bt[2], x, 6); break;
				case 1:  /*SubNext(bt[0], x, 3); SubNext(bt[1], x, 6);*/ break;
				case 3:  SubNext(bt[0], x, 1); /*SubNext(bt[1], x, 6);*/ break;
				case 6:  SubNext(bt[0], x, 1); /*SubNext(bt[1], x, 3);*/ break;
			}
			break;
		case 3:
			switch( p ){
				case -1: SubNext(bt[0], x, 2); SubNext(bt[1], x, 7); break;
				case 2:  /*SubNext(bt[0], x, 7);*/ break;
				case 7:  SubNext(bt[0], x, 2); break;
			}
			break;
		case 4:
			switch( p ){
				case -1: SubNext(bt[0], x, 0); SubNext(bt[1], x, 5); break;
				case 0:  SubNext(bt[0], x, 5); break;
				case 5:  /*SubNext(bt[0], x, 0);*/ break;
			}
			break;
		case 5:
			switch( p ){
				case -1: SubNext(bt[0], x, 1); SubNext(bt[1], x, 4); SubNext(bt[2], x, 6); break;
				case 1:  /*SubNext(bt[0], x, 4);*/ SubNext(bt[1], x, 6); break;
				case 4:  SubNext(bt[0], x, 1); SubNext(bt[1], x, 6); break;
				case 6:  SubNext(bt[0], x, 1); /*SubNext(bt[1], x, 4);*/ break;
			}
			break;
		case 6:
			switch( p ){
				case -1: SubNext(bt[0], x, 2); SubNext(bt[1], x, 5); SubNext(bt[2], x, 7); break;
				case 2:  /*SubNext(bt[0], x, 5);*/ SubNext(bt[1], x, 7); break;
				case 5:  SubNext(bt[0], x, 2); SubNext(bt[1], x, 7); break;
				case 7:  SubNext(bt[0], x, 2); /*SubNext(bt[1], x, 5);*/ break;
			}
			break;
		case 7:
			switch( p ){
				case -1: SubNext(bt[0], x, 3); SubNext(bt[1], x, 6); break;
				case 3:  /*SubNext(bt[0], x, 6);*/ break;
				case 6:  SubNext(bt[0], x, 3); break;
			}
			break;
	}
	for(int i = 0; i < 3; ++i)
		if( bt[i].prev >= 0 )
			vNext.push_back( bt[i] );
}
int BFS( Board &st ){
	queue< Board > q;

	q.push( st );

	while( !q.empty() ){
		Board b = q.front();
		vector<Board> next;

		if( b.check() ) return b.depth;

		q.pop();
		Next( b, next );
		for( unsigned int i = 0; i < next.size(); ++i )
			q.push( next[i] );
	}
	return -1;
}
int main(void){
	while( true ){
		Board bst;
		
		for(int i = 0; i < MAX; ++i){
			int t;

			cin >> t;
			if( cin.eof() ) return 0;
			bst.num[i] = t;
			bst.pos[t] = i;	
		}
		bst.prev = -1;
		bst.depth = 0;
		cout << BFS( bst ) << '\n';
	}
	return 0;
}