# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

map<vector<int>, int> memo;

int dm[4] = { -1,1,-4,4 };
void bfs() {
	vector<int> d = { 0,1,2,3,4,5,6,7 };
	memo[d] = 0;
	queue<vector<int>> que;
	que.push(d);
	while (que.size()) {
		vector<int> v = que.front();
		REP(i, 8)cout << v[i] << " ";
		cout << endl;
		que.pop();
		int zero;
		REP(i, 8)if (v[i] == 0)zero = i;
		vector<int> copy = v;
		REP(i, 4) {
			if (zero + dm[i] >= 0 && zero + dm[i] < 8) {
				swap(copy[zero], copy[zero + dm[i]]);
				if (memo.find(copy) == memo.end()) {
					memo[copy] = memo[v] + 1;
					REP(j, 8)cout << v[j] << " ";
					cout << endl;
					que.push(copy);
				}
				swap(copy[zero], copy[zero + dm[i]]);
			}
		}
	}
}

int main() {
	bfs();
	int a, b, c, d, e, f, g, h;
	while (cin >> a >> b >> c >> d >> e >> f >> g >> h) {
		cout << memo[{a, b, c, d, e, f, g, h}] << endl;
	}
}