#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1,0,1,0}; const int dy[] = {0,1,0,-1};

const int MAX_N = 76543211;
int G[MAX_N];
const int INF = (int)1e9;

struct P{
	int n, p, turn;
};

int main() {
	int goal = 1234567;

	vi card(8);
	while(cin >> card[0] >> card[1] >> card[2] >> card[3] >> card[4] >> card[5] >> card[6] >> card[7]){
		int ps = -1;
		int n = 0;
		REP(i, 8){
			n = n*10 + card[i];
			if(card[i] == 0) ps = i;
		}

		fill_n((int *)G, MAX_N, INF);
		queue<P> que;
		que.push({n, ps, 0});
//		G[n] = 0;

		while(!que.empty()){
			P p = que.front(); que.pop();
			if(p.n == goal){
				cout << p.turn << endl;
				break;
			}
			REP(i, 8){
				card[7-i]=p.n%10;
				p.n/=10;
			}
			p.turn++;

			vi card2;
			//上
			card2 = card;
			if(p.p - 4 >= 0){
				swap(card2[p.p-4], card2[p.p]);
				int n = 0;
				REP(i, 8){
					n = n*10 + card2[i];
				}
				if (G[n] > p.turn){
					G[n] = p.turn;
					que.push({n, p.p-4, p.turn});
				}
			}
			//下
			card2 = card;
			if(p.p + 4 < 8){
				swap(card2[p.p+4], card2[p.p]);
				int n = 0;
				REP(i, 8){
					n = n*10 + card2[i];
				}
				if (G[n] > p.turn){
					G[n] = p.turn;
					que.push({n, p.p+4, p.turn});
				}
			}
			//左
			card2 = card;
			if(p.p%4 - 1 >= 0){
				swap(card2[p.p-1], card2[p.p]);
				int n = 0;
				REP(i, 8){
					n = n*10 + card2[i];
				}
				if (G[n] > p.turn){
					G[n] = p.turn;
					que.push({n, p.p-1, p.turn});
				}
			}
			//右
			card2 = card;
			if(p.p%4 + 1 < 4){
				swap(card2[p.p+1], card2[p.p]);
				int n = 0;
				REP(i, 8){
					n = n*10 + card2[i];
				}
				if (G[n] > p.turn){
					G[n] = p.turn;
					que.push({n, p.p+1, p.turn});
				}
			}
		}
	}
}