#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<sstream>
#include<stack>
#include<functional>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF=0x3f3f3f3f;
const int MAX_N = 9;
const int dir[] = {1, -1, 4, -4};
const int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
const char s[] = "01234567";
int ans[40320];
struct Node{
    int x, step;
    char g[MAX_N];
    Node();
    Node(int a, int b, const char* c):x(a), step(b){
        for(int i = 0; i < 8; i++)g[i] = c[i];
    }
    Node(const Node &c){
        x = c.x;
        step = c.step;
        for(int i = 0; i < 8; i++)g[i] = c.g[i];
    };
    Node operator = (const Node &c){
        x = c.x;
        step = c.step;
        for(int i = 0; i < 8; i++)g[i] = c.g[i];
    }
};
//康拓展?
int cantor(const char * str){
   int res = 0;
   for(int i = 0; i < 8; i++){
        int t = 0;
        for(int j = i + 1; j < 8; j++)
            if(str[j] < str[i])t++;
        res += (t * fac[8 - i - 1]);
   }
   return res;
}

void bfs(){
    queue<Node> q;
    q.push(Node(0, 0, s));
    ans[cantor(s)] = 0;
    while(q.size()){
        Node cur = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            Node nxt(cur);
            nxt.x = cur.x + dir[i];
            if(nxt.x >= 0 && nxt.x < 8){
                swap(nxt.g[nxt.x], nxt.g[cur.x]);
                int ctor = cantor(&nxt.g[0]);
                if(ans[ctor] == -1){
                    nxt.step++;
                    ans[ctor] = nxt.step;
                    q.push(nxt);
                }
            }
        }
    }
}
int main()
{
	#ifdef LOCAL
		freopen("in.txt","r",stdin);
	//	freopen("out.txt","w",stdout);
	#endif
	//ios::sync_with_stdio(false);
	fill(ans, ans + fac[8], -1);
	bfs();
	string str;
	while(getline(cin, str)){
        str.erase(remove(str.begin(), str.end(), ' '), str.end());
        char str1[8];
        for(int i = 0; i < 8; i++)str1[i] = str[i];
        cout << ans[cantor(str1)] << endl;
	}
	return 0;
}