#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#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;

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));
}
*/

#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(const B &b){
  int ret = 0;
  REP(y,2) REP(x,4)
    ret = ret + ABS(py[b.b[y][x]]-y) + ABS(px[b.b[y][x]]-x);
  return ret;
}

bool operator < (const B &a, const B &b) { return true; }

int hash(const B &b){
  int base = 1;
  int ret = 0;
  REP(i,8){
    ret += base * b.b[i/4][i%4];
    base *= 7;
  }
  return ret;
}

int main(){
  int tmp;
  B ans;
  while(cin >> tmp){
    B b;
    b.b[0][0] = tmp;
    REP(i,7){
      cin >> tmp;
      b.b[(i+1)/4][(i+1)%4] = tmp;
    }
    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 = hash(bb);

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

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

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