#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <string>
#include <map>
using namespace std;
#define FOR(I,N) for(int I = 0; I < (int)(N); I++)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;

int mv[4] = {1, -1, 4, -4};

struct Data{
	string panel;
	int space, cost;
	Data(string panel_, int space_, int cost_){
		panel = panel_;
		space = space_;
		cost = cost_;
	}
	Data(){}
};
bool check(string s){
	FOR(i, 8){
		if(s[i] != '0' + i)return false;
	}
	return true;
}

int bfs(Data d){
	map<string, bool> mp;
	queue<Data> que;
	que.push(d);
	mp[d.panel] = 1;
	while(!que.empty()){
		Data now = que.front();
		que.pop();
		int cost = now.cost;

		if(check(now.panel)){
			return cost;
		}

		int idx1 = now.space;
		FOR(i, 4){
			int idx2 = idx1 + mv[i];
			if(idx2 == 4 && idx1 == 3)continue;
			if(idx2 == 3 && idx1 == 4)continue;
			if(idx2 < 0 || idx2 >= 8)continue;
			//??????????\?
			string next = now.panel;
			next[idx1] = now.panel[idx2];
			next[idx2] = now.panel[idx1];

			Data next_stat;
			next_stat.panel = next;
			next_stat.space = idx2;
			next_stat.cost = cost+1;

			if(mp.count(next_stat.panel))continue;

			mp[next_stat.panel] = 1;
			que.push(next_stat);
		}

	}
	return 0;
}


int main(void){
	int n, space;
	string c;

	while(cin >> c){
		string panel;
		panel += c;
		if(c[0] == '0')space = 0;
		for(int i = 1; i <= 7; i++){
			string S;
			cin >> S;
			panel += S;
			if(c[i] == '0')space = i;
		}
		cout << panel << endl;
		Data d(panel, space, 0);
		FIN(bfs(d));
	}
	return 0;	
}