#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <ostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000000
#define rep(i,a,b) for (int i=(a);i<(b);i++)
#define rev(i,a,b) for (int i=(a)-1;i>=b;i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef queue< pair<int, int> > qpii;
typedef vector<int> vi;
typedef vector<int, int> vii;
typedef vector<string> vs;
typedef vector< pair<int, int> > vpii;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int num[8];
int cnt[8];

bool check() {
	rep(i,0,7) if(i != num[i]) return false;
	return true;
}

int bfs() {
	rep(i,0,8) cnt[i] = 0;
	int d[4] = {4, -4, -1, 1};

	qi q;
	rep(i,0,8)
		if(i != num[i]) {
			q.push(i);
			cnt[i] = 0;
		}

	while(q.size()) {
		int n = q.front(); q.pop();

		if(check()) break;
		//if(n == num[n]) continue;
		
		rep(i,0,4) {
			int mn = n + d[i];
			if(0<=mn && mn<8 && cnt[mn]<=cnt[n]) {
				if(num[mn] < num[n] && d[i] < 0) continue;
				else if(num[mn] > num[n] && d[i] > 0) continue;
				swap(num[mn], num[n]);
				
				
				cnt[mn] = cnt[n] + 1;
				if(n != num[mn]) q.push(n);
				if(mn != num[n]) q.push(mn);
				break;
			}
		}
	}

	int ans =0;
	rep(i,0,8) {
		if(cnt[i] == -1) continue;
		ans += cnt[i];
	}
	return ans;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(scanf("%d %d %d %d %d %d %d %d"
		  ,&num[0], &num[1], &num[2], &num[3], &num[4], &num[5], &num[6], &num[7]) != EOF) {
		
		cout << bfs() << endl;
		
	}
	return 0;
}