#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int vectonum(vector<int> vec){
  int ret = 0;
  REP(i,8){
    ret += pow(8,i) * vec[i];
  }
  return ret;
}
int dist[40321][40321]; //8!=40320
int main(void){
  REP(i,40321)REP(j,40321) dist[i][j] = (i!=j)?1e8:0;
  map<int,int> numtoindex;
  vector<int> seq(8);
  REP(i,8) seq[i] = i;
  int incindex=0;
  do{
    numtoindex[vectonum(seq)] = incindex++;
  }while(next_permutation(seq.begin(),seq.end()));
  REP(i,8) seq[i] = i;
  do{
    int from = numtoindex[vectonum(seq)];
    int to;
    int zeroindex; REP(i,8)if(!seq[i])zeroindex = i;
    vector<int> tmpseq;
    if(zeroindex%4!=3){
      tmpseq = seq;
      swap(tmpseq[zeroindex],tmpseq[zeroindex+1]);
      to = numtoindex[vectonum(tmpseq)];
      dist[from][to] = dist[to][from] = 1;
    }
    if(zeroindex%4!=0){
      tmpseq = seq;
      swap(tmpseq[zeroindex],tmpseq[zeroindex-1]);
      to = numtoindex[vectonum(tmpseq)];
      dist[from][to] = dist[to][from] = 1;
    }
    if(zeroindex<4){
      tmpseq = seq;
      swap(tmpseq[zeroindex],tmpseq[zeroindex+4]);
      to = numtoindex[vectonum(tmpseq)];
      dist[from][to] = dist[to][from] = 1;
    }
    if(zeroindex>3){
      tmpseq = seq;
      swap(tmpseq[zeroindex],tmpseq[zeroindex-4]);
      to = numtoindex[vectonum(tmpseq)];
      dist[from][to] = dist[to][from] = 1;
    }
  }while(next_permutation(seq.begin(),seq.end()));
  REP(k,40321)REP(i,40321)REP(j,40321)dist[i][j] = min((int)dist[i][j],dist[i][k]+dist[k][j]);
  string in;
  while(getline(cin,in)){
    stringstream ss(in);
    vector<int> inseq(8);
    REP(i,8)ss>>inseq[i];
    int from = numtoindex[vectonum(inseq)];
    cout<<dist[from][0]<<endl;
  }
  return 0;
}