#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstdlib>
#define INF 100000000
#define _INF -100000000
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;

struct dat {
	string s;
	int pos;
	int val;
};
map<string, int> mp;
int dx[4] = { -1,1,4,-4 };

void solve() {
	queue<dat> que;
	que.push({ "0 1 2 3 4 5 6 7", 0, 0 });
	mp["0 1 2 3 4 5 6 7"] = 0;
	while (que.size()) {
		dat focus = que.front(); que.pop();
		Loop(k, 4) {
			int a = focus.pos + dx[k];
			if (a < 0 || 7 < a) continue;
			string s = focus.s;
			swap(s[focus.pos * 2], s[a * 2]);
			if (mp[s] > 0) continue;
			if (s == "0 1 2 3 4 5 6 7") continue;
			int x = mp[focus.s] + 1;
			mp[s] = x;
			que.push({ s,a,x });
		}
	}
}

int main() {
	solve();
	while (1) {
		string s;
		getline(cin,s);
		cout << mp[s] << endl;
	}
	return 0;
}