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

using namespace std;

struct Board{
	int key;
	int num[MAX];
	int pos[MAX];
	int prev;
	void refreshKey(){
		key = num[0] + 1;
		for(int i = 1; i < MAX; ++i){
			key *= 10;
			key += (num[i] + 1);
		}
	}
	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;
	}
	bool operator < (const Board &b) const {
		return key < b.key; 
	}
} ;

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;
}
int BinFind( vector<Board> &v, int from, const Board &b, bool &found ){
	int l = from;
	int r = v.size();
	int m;

	found = false;

	if( r - l == 0 )
		return r;
	else if( r - l == 1 ){
		found = v[r - 1] == b;
		return r;
	}

	if( v[r-1].key < b.key )
		return r;
	else if(v[l].key > b.key )
		return l;

	while( r - 1 > l ){
		m = (l + r) / 2;
		if( b.key > v[m].key )
			l = m;
		else if( b.key < v[m].key )
			r = m;
		else{
			found = true;
			break;
		}
	}
	return m;
}
void Next( vector<Board> &vBoard ){
	int sz = vBoard.size();

	for(int i = 0; i < sz; i++){
		Board bt[3];
		int p, x;
		bool bFound;

		for(int j = 0; j < 3; ++j){
			bt[j] = vBoard[i];
			bt[j].prev = -1;
		}
		x = bt[0].pos[0];
		p = bt[0].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], 2, 1); SubNext(bt[1], 2, 3); SubNext(bt[2], 2, 6); break;
					case 1:  SubNext(bt[0], 2, 3); SubNext(bt[1], 2, 6); break;
					case 3:  SubNext(bt[0], 2, 1); SubNext(bt[1], 2, 6); break;
					case 6:  SubNext(bt[0], 2, 1); SubNext(bt[1], 2, 3); break;
				}
				break;
			case 3:
				switch( p ){
					case -1: SubNext(bt[0], 3, 2); SubNext(bt[1], 3, 7); break;
					case 2:  SubNext(bt[0], 3, 7); break;
					case 7:  SubNext(bt[0], 3, 2); break;
				}
				break;
			case 4:
				switch( p ){
					case -1: SubNext(bt[0], 4, 0); SubNext(bt[1], 4, 5); break;
					case 0:  SubNext(bt[0], 4, 5); break;
					case 5:  SubNext(bt[0], 4, 0); break;
				}
				break;
			case 5:
				switch( p ){
					case -1: SubNext(bt[0], 5, 1); SubNext(bt[1], 5, 4); SubNext(bt[2], 5, 6); break;
					case 1:  SubNext(bt[0], 5, 4); SubNext(bt[1], 5, 6); break;
					case 4:  SubNext(bt[0], 5, 1); SubNext(bt[1], 5, 6); break;
					case 6:  SubNext(bt[0], 5, 1); SubNext(bt[1], 5, 4); break;
				}
				break;
			case 6:
				switch( p ){
					case -1: SubNext(bt[0], 6, 2); SubNext(bt[1], 6, 5); SubNext(bt[2], 6, 7); break;
					case 2:  SubNext(bt[0], 6, 5); SubNext(bt[1], 6, 7); break;
					case 5:  SubNext(bt[0], 6, 2); SubNext(bt[1], 6, 7); break;
					case 7:  SubNext(bt[0], 6, 2); SubNext(bt[1], 6, 5); break;
				}
				break;
			case 7:
				switch( p ){
					case -1: SubNext(bt[0], 7, 3); SubNext(bt[1], 7, 6); break;
					case 3:  SubNext(bt[0], 7, 6); break;
					case 6:  SubNext(bt[0], 7, 3); break;
				}
				break;
		}
		for(int j = 0; j < 3; ++j){
			if( bt[j].prev >= 0 ){
				bt[j].refreshKey();
				
				int pos = BinFind( vBoard, sz, bt[j], bFound );
				if( !bFound )
					vBoard.insert( vBoard.begin() + pos, bt[j] );
			}
		}
	}
	vBoard.erase( vBoard.begin(), vBoard.begin() + sz );
}

int main(void){
	while( true ){
		int cnt = 0;
		Board btmp;
		vector< Board > vStatus;

		vStatus.push_back( btmp );

		for(int i = 0; i < MAX; ++i){
			int t;

			cin >> t;
			if( cin.eof() ) return 0;
			vStatus[0].num[i] = t;
			vStatus[0].pos[t] = i;	
		}
		vStatus[0].prev = -1;

		while( true ){
			bool bSolved = false;

			for(unsigned int i = 0; i < vStatus.size(); ++i){
				//cout << i << ':';
				vStatus[i].refreshKey();
				//vStatus[i].print();
				if( vStatus[i].check() ){
					bSolved = true;
					break;
				}
			}
			//cout << '\n';
			
			if( bSolved ) break;
			sort( vStatus.begin() , vStatus.end() );
			Next( vStatus );
			cnt++;
		}
		cout << cnt << '\n';
	}
	return 0;
}