#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) string((s).rbegin(), (s).rend())
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;



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

void test(){
}

int main(){


	string s = "01234567";
	map<string, int> m;
	m[s] = 0;
	queue<pair<int, string > > q;
	q.push(MP(0, s));

	while(!q.empty()){
		int pos = q.front().first;
		string s = q.front().second;
		q.pop();

		rep(i, 4){
			int npos = pos+d[i];
			if(npos < 0 || npos >= 8) continue;
			// 3-4は交換不可
			if(pos == 3 && npos == 4) continue;
			if(pos == 4 && npos == 3) continue;
			string t = s;
			swap(t[pos], t[npos]);
			if(m.find(t) != m.end()) continue;
			m[t] = m[s]+1;
			q.push(MP(npos, t));
		}
	}

	while(1){
		char c;
		string s;
		int zeropos;
		rep(i, 8){
			if(!(cin >> c))return 0;
			if(c == '0')zeropos = i;
			s += c;
		}
		cout << m[s] << endl;
	}

	return 0;
}