#include<iostream>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
using namespace std;
struct Puzzle{
	public:
	Puzzle(){zeroIndex = 0; count = 0;}
	void Move(int i){
		if(i == 0){
			swap(puzzle[zeroIndex], puzzle[zeroIndex + 1]);
			zeroIndex = zeroIndex + 1;
		}else if(i == 1){
			swap(puzzle[zeroIndex], puzzle[zeroIndex + 4]);
			zeroIndex = zeroIndex + 4;
		}else if(i == 2){
			swap(puzzle[zeroIndex], puzzle[zeroIndex - 1]);
			zeroIndex = zeroIndex - 1;
		}else if(i == 3){
			swap(puzzle[zeroIndex], puzzle[zeroIndex - 4]);
			zeroIndex = zeroIndex - 4;
		}
		moved = i;
	}
	void SetPuzzle(int* _puzzle){
		for(int i = 0; i < 8; i++){
			puzzle[i] = _puzzle[i];
			if(puzzle[i] == 0){
				zeroIndex = i;
			}
		}
	}
	bool IsFinish(){
		for(int i = 0; i < 8; i++){
			if(puzzle[i] != i){
				return false;
			}
		}
		return true;
	}
	int makeNumber(){
		int val = 0;
		for(int i = 0; i < 7; i++){
			val += puzzle[i] * pow(10, 6 - i);
		}
		return val;
	}
	
	int count;
	int zeroIndex;
	int moved;
	int puzzle[8];
};

int main(){
	while(cin){
		queue<Puzzle> puzzles;
		Puzzle origin;
		int base[8];
		for(int i = 0; i < 8; i++){
			cin >> base[i];
		}
		origin.SetPuzzle(base);
		origin.moved = -1;
		puzzles.push(origin);
		bool* tested = new bool[7654322];
		for(int i = 0; i < 7654322; i++){
			tested[i] = false;
		}
		while(!puzzles.empty()){
			Puzzle current = puzzles.front();
			puzzles.pop();
			if(current.IsFinish()){
				cout << current.count << endl;
				break;
			}
			current.count++;
			/*cout << current.count;
			for(int i = 0; i < 8; i++){
				cout << tested[tested.size() - 1][i] << ',';
			}
			cout << endl;*/

			if(current.zeroIndex != 3 && current.zeroIndex != 7 && current.moved != 2){
				Puzzle right = current;
				right.Move(0);
				int index = right.makeNumber();
				if(!tested[index]){
					puzzles.push(right);
					tested[index] = true;
				}
			}
			
			if(current.zeroIndex <= 3 && current.moved != 3){
				Puzzle down = current;
				down.Move(1);
				int index = down.makeNumber();
				if(!tested[index]){
					puzzles.push(down);
					tested[index] = true;
				}
			}
			
			if(current.zeroIndex != 0 && current.zeroIndex != 4 && current.moved != 0){
				Puzzle left = current;
				left.Move(2);
				int index = left.makeNumber();
				if(!tested[index]){
					puzzles.push(left);
					tested[index] = true;
				}
			}
			
			if(current.zeroIndex >= 4 && current.moved != 1){
				Puzzle up = current;
				up.Move(3);
				int index = up.makeNumber();
				if(!tested[index]){
					puzzles.push(up);
					tested[index] = true;
				}
			}
			//delete[] tested;
		}
	}
	return 0;
}