#pragma warning(disable:4996)
#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)
#define TPL template
#define TNM typename

using ll = long long;
using ull = unsigned long long;
TPL<TNM T> using vec = vector<T>;
TPL<TNM T> using vec2 = vec<vec<T>>;
TPL<TNM T> using vec3 = vec<vec2<T>>;
TPL<TNM T> using vec4 = vec<vec3<T>>;
TPL<TNM T> using vec5 = vec<vec4<T>>;

TPL<TNM V, TNM H> void resize(vector<V>& v, const H h) { v.resize(h); }
TPL<TNM V, TNM H, TNM ... T> inline void resize(vector<V>& v, const H& h, const T ... t) { v.resize(h); for (auto& _v : v) resize(_v, t ...); }
TPL<TNM V, TNM T> void fill(V& x, const T& val) { x = val; }
TPL<TNM V, TNM T> inline void fill(vector<V>& vec, const T& val) { for (auto& v : vec) fill(v, val); }
TPL<TNM T> inline void UNIQUE(vector<T>& vec) { sort(ALL(vec)); vec.erase(unique(ALL(vec)), vec.end()); }

inline bool range1d(int x, int W) { return (x >= 0) && (W > x); }
const int dx[] = { 1, -1, 4, -4 }; //左右上下

struct Node {
	string s;
	int c;
	Node(string _s, int _c) : s(_s), c(_c) {};
};

int main(void) {
	map<string, int> memo;
	queue<Node> q;
	q.push({ "01234567", 0 });
	while (q.size()) {
		Node v = q.front(); q.pop();
		if (memo.count(v.s) != 0)continue;
		memo[v.s] = v.c;
		REP(i, 4) {
			int id = v.s.find('0');
			if (i == 0 && id == 3)continue;
			if (i == 1 && id == 4)continue;
			string t = v.s;
			if (range1d(id + dx[i], 8)) {
				swap(t[id], t[id + dx[i]]);
				q.push({ t, v.c + 1 });
			}
		}
	}
	char c;
	while (cin >> c) {
		string S = "";
		S += c;
		REP(i, 7) {
			cin >> c;
			S += c;
		}
		cout << memo[S] << endl;
	}
	return 0;
}
