#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<stdio.h>
using namespace std;

struct cell {
  int zero, ary, cnt;
};

int exc (int v, int a, int b) {
  a*=4; b*=4;
  int v1 = (v>>a) & 0xf;
  int v2 = (v>>b) & 0xf;
  return v - (v1<<a) + (v2<<a) - (v2<<b) + (v1<<b);
}

int main () {
  int i,bit;
  while(true) {
    queue<cell> q;
    map<int,int> m;
    int zero, sft = 0, ary = 0, sc = 0;
    char c;
    string s = "";
    while(cin>>c){
      s+=c;sc++;
      if(sc == 8)
	break;
    }
    if(sc == 0)
      return 0;
    for(i = 0; i < 8; ++i) {
      bit = s[i]-'0';
      if(bit == 0) zero = i;
      ary += bit << sft;
      sft += 4;
    }
    q.push((cell){zero,ary,0});
    while(!q.empty()){
      cell c = q.front();q.pop();
      int zero = c.zero, ary = c.ary, cnt = c.cnt;
      if(ary == 0x76543210){
	cout<<cnt<<endl;
	break;
      }
      if(m[ary] == 0)
	m[ary] = cnt+1;
      else if(cnt+1 >= m[ary])
	continue;
      int x = zero % 4, y = zero / 4;
      if(x>=1)q.push((cell){zero-1,exc(ary,zero,zero-1),cnt+1});
      if(x<=2)q.push((cell){zero+1,exc(ary,zero,zero+1),cnt+1});
      if(y==1)q.push((cell){zero-4,exc(ary,zero,zero-4),cnt+1});
      if(y==0)q.push((cell){zero+4,exc(ary,zero,zero+4),cnt+1});
    }
  }
}