#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, cnt;
	Data(string panel_, int space_, int cnt_){
		panel = panel_;
		space = space_;
		cnt = cnt_;
	}
	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 data = que.front();
		que.pop();

		if(check(data.panel)) return data.cnt;

		int id1 = data.space;
		FOR(i, 4){
			int id2 = id1 + mv[i];
			if(id2 < 0 || id2 >= 8)continue;
			//??????????\?
			string make_next = data.panel;
			make_next[id1] = data.panel[id2];
			make_next[id2] = data.panel[id1];

			Data next;
			next.panel = make_next;
			next.space = id2;
			next.cnt = data.cnt+1;

			if(mp.count(next.panel) == 0){
				mp[next.panel] = 1;
				que.push(next);
				cout << next.panel << endl;
			}
		}

	}
	return 0;
}


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

	while(cin >> c){
		string panel;
		panel.pb(c);
		FOR(j, 7){
			cin >> c;
			panel.pb(c);
			if(c == '0')space = j;
		}
		Data d(panel, space, 0);
		FIN(bfs(d));
	}
	return 0;	
}