#include<iostream>
#include<map>
#include<vector>
#include<queue>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  vector<vector<int> > goal(2);
  rep (i, 8) goal[i / 4].push_back(i);
  map<vector<vector<int> >, int> mp;
  queue<pair<int, vector<vector<int> > > > que;
  que.push(make_pair(0, goal));
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  while (!que.empty()) {
    pair<int, vector<vector<int> > > now = que.front();
    que.pop();
    if (mp.count(now.second) != 0) continue;
    mp[now.second] = now.first;
    rep (i, 2) rep (j, 4) if (now.second[i][j] == 0) {
      rep (k, 4) {
	if (i + dy[k] < 0 || 2 <= i + dy[k]) continue;
	if (j + dx[k] < 0 || 4 <= j + dx[k]) continue;
	pair<int, vector<vector<int> > > next = now;
	next.first++;
	swap(next.second[i][j], next.second[i + dy[k]][j + dx[k]]);
	que.push(next);
      }
    }
  }
  vector<vector<int> > input(2 ,vector<int>(4));
  while (cin >> input[0][0] >> input[0][1] >> input[0][2] >> input[0][3] >> input[1][0] >> input[1][1] >> input[1][2] >> input[1][3]) cout << mp[input] << endl;
  return 0;
}