#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<string,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
map<string,int> m;
string start="01234567";
int d[4]={1,-1,4,-4};
void bfs()
{
	queue<string> q;
	q.push(start);
	m[start]=0;
	while(!q.empty())
	{
		string a=q.front();
		q.pop();
		int pos=a.find("0");
		for(int i=0;i<4;i++)
		{
			string tmp=a;
			if(pos%4==0&&i==1)continue;
			if(pos%4==3&&i==0)continue;
			int nx=pos+d[i];
			if(!(nx>=0&&nx<=7))continue;
			swap(tmp[pos],tmp[nx]);
			if(m.find(tmp)!=m.end())continue;
			m[tmp]=m[a]+1;
			q.push(tmp);
		}
	}
	return;
}
int main()
{
	string s;
	bfs();
	while(getline(cin,s))
	{
		s.erase(remove(s.begin(),s.end(),' '),s.end());
		cout << m[s] << endl;
	}
	return 0;
}