#include<iostream>
#include<string>
#include<map>
using namespace std;

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

#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)

#define N 8
#define INF 1000000007

map<string, int> memo;
int dfs(string s, int i) {
  if(memo.count(s)) return memo[s];
  memo[s] = INF;
  int ret = INF;
  if((i%4)>0) {
    s[i] = s[i-1]; s[i-1] = '0';
    int r0 = dfs(s, i-1)+1;
    s[i-1] = s[i]; s[i] = '0';
    ret = min(ret, r0);
  }
  if((i%4)<3) {
    s[i] = s[i+1]; s[i+1] = '0';
    int r1 = dfs(s, i+1)+1;
    s[i+1] = s[i]; s[i] = '0';
    ret = min(ret, r1);
  }
  int p = i<4 ? 4 : -4;
  s[i] = s[i+p]; s[i+p] = '0';
  int r2 = dfs(s, i+p)+1;
  s[i+p] = s[i]; s[i] = '0';
  ret = min(ret, r2);
  return memo[s] = ret;
}

int main() {
  int x[N], idx;
  memo["01234567"] = 0;
  while(cin >> x[0]) {
    rep(i, N-1) cin >> x[i+1];
    string s;
    rep(i, N) {
      s += x[i] + '0';
      if(x[i] == 0) idx = i;
    }
    int ret = dfs(s, idx);
    cout << ret << endl;
  }
}