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

int database[40320] = {0};

struct Puzzle{
	int cont[8];
	int space,te;
	bool operator < (const Puzzle &p) const{
		for(int i = 0;i < 8;i++){
			if(cont[i] == p.cont[i]) continue;
			return cont[i] > p.cont[i];
		}
		return false;
	}
};

int input_number;
static const int dx[4] = {-1,0,1,0};
static const int dy[4] = {0,-1,0,1};

int change_number(int *board){
  int work[8];
  static int fact_table[8] = {
    5040, 720, 120, 24, 6, 2, 1, 0,
  };
  int value = 0;
  
  for(int i = 0;i < 8;i++){
  	work[i] = board[i];
  }
  
  for(int j = 0;j < 7;j++){
    value += fact_table[j]*work[j];
    for(int k = j+1;k < 8;k++){
      if(work[j] < work[k]) work[k]--;
    }
  }
  
  return value;
}

void bfs(Puzzle s){
	queue<Puzzle> Q;
	map<Puzzle, bool> V;
	Puzzle u,v;
	
	s.te = 0;
	Q.push(s);
	V[s] = true;
	
	while(!Q.empty()){
		u = Q.front();
		Q.pop();
		
		int tmp = change_number(u.cont);
		
		if(!tmp){
			cout << u.te << endl;
			database[input_number] = u.te;
			return;
		}else{
			if(database[tmp]){
				cout << u.te+database[tmp] << endl;
				database[input_number] = u.te+database[tmp];
				return;
			}
		}
		
		int sx,sy,tx,ty;
		sx = u.space%4;
		sy = u.space/4;
		
		for(int r = 0;r < 4;r++){
			tx = sx+dx[r];
			ty = sy+dy[r];
			if(tx < 0 || ty < 0 || tx >= 4 || ty >= 2) continue;
			
			v = u;
			v.cont[u.space] = u.cont[ty*4+tx];
			v.cont[ty*4+tx] = 0;
			v.space = ty*4+tx;
			if(!V[v]){
				V[v] = true;
				v.te += 1;
				Q.push(v);
			}
		}
	}
}

int main() {
	Puzzle in;
	
	while(cin >> in.cont[0]){
		if(!in.cont[0]) in.space = 0;
		for(int i = 1;i < 8;i++){
			cin >> in.cont[i];
			if(!in.cont[i]) in.space = i;
		}

		input_number = change_number(in.cont);
		if(!input_number){
			cout << "0" << endl;
		}else{
			bfs(in);
		}
	}
	
	return 0;
}