#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1; i>=0; i--)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl
#define F first
#define S second
typedef long long ll;
typedef pair<int,string> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,-4,1,4};

int main() {
  map<string,int> d;
  string s="01234567";
  d[s]=0;
  queue<P> que;
  que.push(P(0,s));
  while(!que.empty()) {
    P p=que.front();
    que.pop();
    int x=p.F;
    s=p.S;
    rep(i,4) {
      int y=x+dx[i];
      if(y<0 || y>=8) continue;
      if(i==2 && y==4) continue;
      if(i==0 && y==3) continue;
      string t=s;
      swap(t[x],t[y]);
      if(d.count(t)) continue;
      d[t]=d[s]+1;
      que.push(P(y,t));
    }
  }
  
  char c;
  while(cin >> c) {
    s="";
    s+=c;
    REP(i,1,8) {
      cin >> c;
      s+=c;
    }
    pr(d[s]);
  }
  return 0;
}
