#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#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()

using namespace std;

typedef unsigned int uint;
typedef uint B;

const uint mask = 0x07;

inline int get(uint d, int i){
  return (d >> (i*3)) & mask;
}

inline void set(uint &d, int i, int n){
  d &= ~(mask << (i*3));
  d |= (n << (3*i));
}

inline int get2(uint d, int x, int y){
  return get(d, x+y*4);
}
inline void set2(uint &d, int x, int y, int n){
  set(d, x+y*4, n);
}

#define ABS(x) ((x)<0?-(x):(x))

//struct B { char b[2][4]; };

int py[8] = { 0,0,0,0,1,1,1,1 };
int px[8] = { 0,1,2,3,0,1,2,3 };

int manhattan(B b){
  int ret = 0;
  REP(y,2) REP(x,4)
    ret = ret + ABS(py[get2(b,x,y)]-y) + ABS(px[get2(b,x,y)]-x);
  return ret;
}

//bool operator < (const B &a, const B &b) { return true; }
inline B swap(B b, int p1, int p2){
  int d1 = get(b, p1);
  int d2 = get(b, p2);
  set(b, p1, d2);
  set(b, p2, d1);
  return b;
}

inline B swap2(B b, int x1, int y1, int x2, int y2){
  return swap(b, x1+y1*4, x2+y2*4);
}

int main(){
  int tmp;
  B ans;
  while(cin >> tmp){
    B b;
    set(b,0,tmp);
    REP(i,7){
      cin >> tmp;
      set(b,i+1,tmp);
    }
    /*
    REP(y,2){
      REP(x,4){
        printf("%d ",get2(b,x,y));
      }
      puts("");
    }
    */
    typedef pair<int,pair<int, B> > type;
    priority_queue<type,vector<type>,greater<type> > pq;
    vector<bool> flag(1<<24, false);
    pq.push(mp(manhattan(b), mp(0, b)));
    while(pq.size()){
      int hy = pq.top().f;
      int nn = pq.top().s.f;
      B   bb = pq.top().s.s;
      int mh = hy - nn;
      pq.pop();

      int hh = bb;

      /*
      printf("hy=%d, nn=%d, hh=%d, manhattan = %d :\n",hy,nn,hh,mh);
      REP(y,2){
        REP(x,4){
          printf("%d ",get2(bb,x,y));
        }
        puts("");
      }
      */

      if(flag[hh]) continue;
      flag[hh] = true;

      if(mh == 0){
        cout << nn << endl;
        break;
      }
      nn++;
      REP(y,2) REP(x,4){
        if(get2(bb,x,y) == 0){
          if(y == 0){
            bb = swap2(bb,x,y,x,y+1);
            pq.push(mp(manhattan(bb) + nn, mp(nn, bb)));
            bb = swap2(bb,x,y,x,y+1);
          }else{
            bb = swap2(bb,x,y,x,y-1);
            pq.push(mp(manhattan(bb) + nn, mp(nn, bb)));
            bb = swap2(bb,x,y,x,y-1);
          }
          if(x != 0){
            bb = swap2(bb,x,y,x-1,y);
            pq.push(mp(manhattan(bb) + nn, mp(nn, bb)));
            bb = swap2(bb,x,y,x-1,y);
          }
          if(x != 3){
            bb = swap2(bb,x,y,x+1,y);
            pq.push(mp(manhattan(bb) + nn, mp(nn, bb)));
            bb = swap2(bb,x,y,x+1,y);
          }
        }
      }
    }
  }
}