#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

bool check(int (&board)[4][2], int (&final_board)[4][2]) 
{
  for (int j = 0; j < 2; j++)
    for (int i = 0; i < 4; i++)
      {
	if (board[i][j] != final_board[i][j])
	  return false;
      }
  return true;
}

ll encode(int (&board)[4][2])
{
  ll encoded = 0;
  for (int j = 0; j < 2; j++)
    for (int i = 0; i < 4; i++) 
      {
	encoded += board[i][j];
	encoded *= 10;
      }
  return encoded/10;
}

void decode(ll number, int (&board)[4][2])
{
  for (int j = 1; j >= 0; j--)
    for (int i = 3; i >= 0; i--) 
      {
	board[i][j] = number % 10;
	number /= 10;
      }
}

int main(void){
  map<ll,ll> board_map;
  queue<ll> board_queue;
  queue<int> count_queue;
  board_map[1234567L] = 0;
  board_queue.push(1234567L);
  count_queue.push(0);
  int count;
  int board[4][2];
while (!board_queue.empty())
    {
      ll board_number = board_queue.front();
      decode(board_number, board);
      count = count_queue.front();
      //cout << board_number << endl;
      int i_zero, j_zero;
      for (int j = 0; j < 2; j++)
	for (int i = 0; i < 4; i++)
	  if (board[i][j] == 0)
	    {
	      i_zero = i;
	      j_zero = j;
	  }
      board_queue.pop();
      count_queue.pop();
      count++;
      //left
      if (i_zero > 0)
	{
	  int board_left[4][2];
	  for (int j = 0; j < 2; j++)
	    for (int i = 0; i < 4; i++)
	      board_left[i][j] = board[i][j];
	  board_left[i_zero][j_zero] = board_left[i_zero-1][j_zero];
	  board_left[i_zero-1][j_zero] = 0;
	  ll encoded_left = encode(board_left);
	  if (board_map.find(encoded_left)==board_map.end())
	    {
	      board_queue.push(encoded_left);
	      board_map[encoded_left] = count;
	      count_queue.push(count);
	    }
	}
      //right
      if (i_zero < 3)
	{
	  int board_left[4][2];
	  for (int j = 0; j < 2; j++)
	    for (int i = 0; i < 4; i++)
	      board_left[i][j] = board[i][j];
	  board_left[i_zero][j_zero] = board_left[i_zero+1][j_zero];
	  board_left[i_zero+1][j_zero] = 0;
	  ll encoded_left = encode(board_left);
	  if (board_map.find(encoded_left)==board_map.end())
	    {
	      board_queue.push(encoded_left);
	      board_map[encoded_left] = count;
	      count_queue.push(count);
	    }
	}
      //up
      if (j_zero == 1)
	{
	  int board_left[4][2];
	  for (int j = 0; j < 2; j++)
	    for (int i = 0; i < 4; i++)
	      board_left[i][j] = board[i][j];
	  board_left[i_zero][j_zero] = board_left[i_zero][j_zero-1];
	  board_left[i_zero][j_zero-1] = 0;
	  ll encoded_left = encode(board_left);
	  if (board_map.find(encoded_left)==board_map.end())
	    {
	      board_queue.push(encoded_left);
	      board_map[encoded_left] = count;
	      count_queue.push(count);
	    }
	}
      //down
      if (j_zero == 0)
	{
	  int board_left[4][2];
	  for (int j = 0; j < 2; j++)
	    for (int i = 0; i < 4; i++)
	      board_left[i][j] = board[i][j];
	  board_left[i_zero][j_zero] = board_left[i_zero][j_zero+1];
	  board_left[i_zero][j_zero+1] = 0;
	  ll encoded_left = encode(board_left);
	  if (board_map.find(encoded_left)==board_map.end())
	    {
	      board_queue.push(encoded_left);
	      board_map[encoded_left] = count;
	      count_queue.push(count);
	    }
	}      
    }
  
  while (cin) 
    {
      int final_board[4][2];
      for (int j = 0; j <2; j++)
	for (int i = 0; i < 4; i++)
	  cin >> final_board[i][j];
      cout << board_map[encode(final_board)] << endl;
    }
}