#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

typedef pair<string,int> P;
unordered_map<string, int> m;
const int d[] = {-4, 1, 4, -1};

void f() {
  queue<P> Q;
  Q.push(P(string("01234567"), 0));
  while (!Q.empty()) {
    P p = Q.front(); Q.pop();
    int cost = p.second;
    if (m.count(p.first)) continue;
    m.insert(P(p.first, cost));
    int zidx;
    for (int i = 0; i < 8; ++i)
      if (p.first[i] == '0')
        zidx = i;
    for (int i = 0; i < 4; ++i) {
      if (zidx == 3 && (i == 1 || i == 2)) continue;
      if (zidx == 4 && (i == 3 || i == 4)) continue;
      int tidx = zidx + d[i];
      if (tidx < 0 || 8 <= tidx) continue;
      string copy = p.first;
      swap(copy[zidx], copy[tidx]);
      Q.push(P(copy, cost+1));
    }
  }
}


int main() {
  f();
  int t;
  while (cin >> t) {
    string s = "";
    s += '0'+t;
    for (int i = 1; i <= 7; ++i) {
      cin >> t;
      s += '0'+t;
    }
    cout << m[s] << endl;
  }
  return 0;
}