#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                                                  it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1,0,1,0};
const int dx[] = {0,1,0,-1};

const double EPS = 1e-9;
const int days[]     = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main(){
    // 01234567
    string final="01234567";

    map<string,int> M;
    set<string> S;
    queue<pair<string,int> > Q;
    Q.push(mp(final,0));
    while(!Q.empty()){
        string s = Q.front().first;
        int ini = Q.front().second;
        Q.pop();
        if(S.find(s) != S.end()) continue;
        S.insert(s);
        M[s] = ini;
        int zerop = 0;
        rep(i,8) if(s[i] == '0') zerop = i;
        rep(i,4){
            int newp = zerop + dx[i];
            newp += 4*dy[i];
            if(zerop == 3 and newp == 4) continue;
            if(zerop == 4 and newp == 3) continue;
            if(0 <= newp and newp <= 7){
                string news = s;
                swap(news[zerop],news[newp]);
                Q.push(mp(news,ini+1));
            }
        }
    }
    string s;
    while(getline(cin,s)){
        stringstream ss;
        ss << s;
        string inp;
        rep(i,8){
            char c;
            ss >> c;
            inp.push_back(c);
        }
        cout << M[inp] << endl;
    }
    return 0;
}