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

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;
	}
};

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

bool isTarget(Puzzle puzzle){
	for(int i = 0;i < 8;i++){
		if(puzzle.cont[i] != i) return false;
	}
	return true;
}

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();
		if(isTarget(u)){
			cout << u.te << endl;
			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 >= 2 || ty >= 4) continue;
			
			v = u;
			v.cont[u.space] = u.cont[tx*4+ty];
			v.cont[tx*4+ty] = 0;
			v.space = tx*4+ty;
			if(!V[v]){
				V[v] = true;
				v.te += 1;
				Q.push(v);
			}
		}
	}
	
	cout << "unsolvable" << endl;
}

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