#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

bool is_end(const vi& a)
{
	rep(i,8)
		if(a[i]!=i)
			return false;
	return true;
}

ostream& operator<<(ostream& os,const vi& a)
{
	rep(i,a.size())
		os<<a[i]<<(i==a.size()-1?"":" ");
	return os;
}

int solve(const vi& a)
{
	queue<pair<vi,int> > q;
	set<vi> memo;
	q.push(mp(a,0));
	while(q.size()){
		vi cur=q.front().first;
		int cur_count=q.front().second;
		q.pop();
		if(memo.count(cur))
			continue;
		memo.insert(cur);
		
		if(is_end(cur))
			return cur_count;
		int ci=-1,cj=-1;
		rep(i,8)
			if(cur[i]==0)
				ci=i/4,cj=i%4;
		int di[]={-1,1,0,0};
		int dj[]={0,0,-1,1};
		rep(i,4){
			int ni=ci+di[i],nj=cj+dj[i];
			if(ni<0 || 2<=ni || nj<0 || 4<=nj)
				continue;
			vi temp=cur;
			swap(temp[ci*4+cj],temp[ni*4+nj]);
			q.push(mp(temp,cur_count+1));
		}
	}
	return -1;	// dummy
}

int main()
{
	for(string s;getline(cin,s),s.size()==15;){
		istringstream iss(s);
		vi a(8);
		rep(i,8)
			iss>>a[i];
		cout<<solve(a)<<endl;
	}
	
	return 0;
}