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

using namespace std;
int dx[4] = {1, -1, 4, -4};
map<string,int> step;


void bfs(void)
{
  queue<string> state;
  state.push("01234567");
  while(!state.empty())
  {
    string v=state.front();
    state.pop();
    int pos = 0;
    for(int i = 0; i < 8; ++i)
	{
      if(v[i] == '0')
	  {
		  pos = i;
	  }
	}

    for(int i = 0; i < 4; ++i)
	{
      if(0 <= pos + dx[i] && pos + dx[i] < 8 && !(pos == 3 && i == 0) && !(pos == 4 && i == 1))
	  {
		string u = v;
		swap(u[pos], u[pos + dx[i]]);

		if(step[u] == 0)
		{
		  state.push(u);
		  step[u] = step[v] + 1;
		}
      }
    }
  }
}

int main()
{
  step["01234567"] = 1;
  bfs();
  string s;
  while(cin >> s)
  {
	char c;
    for(int i = 0; i < 7; ++i)
	{
		cin >> c;
		s += c;
    }
    cout << step[s] - 1 << endl;
  }
  return(0);
}