#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back

typedef struct{
  vi pat;
  int te,pos0;
}node;

main(){
  vector<int> goal;
  rep(i,8)goal.pb(i);

  int s;
  while(cin>>s){
    vector<int> start;
    start.pb(s);
    int pos0=7;
    rep(i,7){
      if(!s)pos0=i;
      cin>>s;
      start.pb(s);
    }

    int ans;
    set<vector<int> > app;
    app.insert(start);
    queue<node > Q;
    node nstart={start,0,pos0};
    Q.push(nstart);

    while(!Q.empty()){
      vector<int> tvi=Q.front().pat;
      int te=Q.front().te,pos0=Q.front().pos0;
      Q.pop();
      if(tvi==goal){
        ans=te;
        break;
      }
      vi n=tvi;

      if(pos0%4){
        swap(n[pos0],n[pos0-1]);
        if(app.count(n)==0){
          app.insert(n);
          Q.push((node){n,te+1,pos0-1});
        }
      }

      n=tvi;
      if(pos0%4!=3){
        swap(n[pos0],n[pos0+1]);
        if(app.count(n)==0){
          app.insert(n);
          Q.push((node){n,te+1,pos0+1});
        }
      }

      n=tvi;
      swap(n[(pos0+4)%8],n[pos0]);
      if(app.count(n)==0){
        app.insert(n);
        Q.push((node){n,te+1,(pos0+4)%8});
      }
    }
    cout<<ans<<endl;
  }
}