#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;
    rep(i,8) final.push_back('0'+i);

    vector<int> V;
    string s;
    while(getline(cin,s)){
        stringstream ss;
        ss << s;
        string first;
        rep(i,8){
            char c;
            ss >> c;
            first.push_back(c);
        }
        set<string> AL;
        queue<pair<string,int> > Q;
        Q.push(mp(first,0));
        int ok = 0;
        while(!Q.empty()){
            string inp = Q.front().first;
            int ini = Q.front().second;

            Q.pop();
            if(AL.find(inp) != AL.end()) continue;
            AL.insert(inp);

            if(inp == final) {
                ok = ini;
                break;
            }
            int zerop = 0;
            for(int i=0;i<8;i++) if(inp[i] == '0') zerop = i;
            // up right down left
            for(int i=0;i<4;i++){
                int newp = zerop;
                newp += dy[i] * 4;
                newp += dx[i];
                if(zerop == 4 and newp == 3) continue;
                if(zerop == 3 and newp == 4) continue;
                if(0 <= newp and newp <= 7){
                    string news = inp;
                    swap(news[zerop],news[newp]);
                    Q.push(mp(news,ini+1));
                }
            }
        }
        cout << ok << endl;
    }
    return 0;
}