#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

int toInt(const string &s) {
  int res = 0;
  for(int i = 0; i < s.size(); ++i) {
    res = res * 10 + (s[i]-'0');
  }
  return res;
}

map<int,int> cost;

void bfs() {
  const string s = "01234567";
  static int di[] = {0,1,0,-1};
  static int dj[] = {1,0,-1,0};
  queue<string> que;
  que.push(s);
  cost[toInt(s)] = 0;
  while(que.size()) {
    const string n = que.front();
    const int in = toInt(n);
    que.pop();
    const int p = n.find('0');
    const int pi = p / 4;
    const int pj = p % 4;
    for(int k = 0; k < 4; ++k) {
      int qi = pi + di[k];
      int qj = pj + dj[k];
      if(qi < 0 || qi >= 2) continue;
      if(qj < 0 || qj >= 4) continue;
      int q = qi * 4 + qj;
      string nn = n;
      swap(nn[p], nn[q]);
      int inn = toInt(nn);
      if(cost.count(inn)) continue;
      que.push(nn);
      cost[inn] = cost[in] + 1;
    }
  }
}

int main() {
  bfs();
  while(1) {
    string s(8, ' ');
    for(int i = 0; i < 8; ++i) {
      if(cin >> s[i]) ; else return 0;
    }
    cout << cost[toInt(s)] << endl;
  }
  return 0;
}