#include <iostream>
#include <string>
#include <queue>
#include <map>

using namespace std;
typedef pair<int, int> info;
typedef pair<string, info> data;

bool isInside( int index ){
  return ( 0 <= index && index < 8 );
}

int main(){

  while(!cin.eof()){
    string goal = "";
    map<string, bool> check_list;
    char num;

    for(int i = 0; i < 8; i++){
      cin >> num;
      goal += num;
    }

    queue<data> que;
    que.push(data("01234567", info(0, 0)));

    while( !que.empty() ){
      data d = que.front(); que.pop();
      if( check_list[d.first] ) continue;

      string s = d.first;
      int index = d.second.first;
      int count = d.second.second;

      if( s == goal ){
        cout << count << endl;
        break;
      }

      check_list[s] = true;

      if( index != 3 && isInside( index+1 ) ){
        string ns = s;
        ns[index] = ns[index+1];
        ns[index+1] = '0';
        que.push(data(ns, info(index+1, count+1)));
      }
      if( isInside( index+4 ) ){
        string ns = s;
        ns[index] = ns[index+4];
        ns[index+4] = '0';
        que.push(data(ns, info(index+4, count+1)));
      }
      if( index != 4 && isInside( index-1 ) ){
        string ns = s;
        ns[index] = ns[index-1];
        ns[index-1] = '0';
        que.push(data(ns, info(index-1, count+1)));
      }
      if( isInside( index-4 ) ){
        string ns = s;
        ns[index] = ns[index-4];
        ns[index-4] = '0';
        que.push(data(ns, info(index-4, count+1)));
      }
    }
  }

  return 0;
}