#include <iostream>
#include <map>
#include <queue>
#include <string>

#define rep(i, b) for(int i = 0; i < b; i++)

using namespace std;

int main(){
    map<string,int> dp;
    string str = "01234567";

    dp[str] = 0;
    queue<string> bfs;
    bfs.push(str);
    while(!bfs.empty()){
        bool flag = true;
        str = bfs.front();
        bfs.pop();
        int t;
        rep(i,8) if(str[i] == '0') t = i;
        
        if(t>0 && t!=4){
            string tm = str;
            swap(tm[t],tm[t-1]);
            if(dp.find(tm) == dp.end()){
                bfs.push(tm);
                dp[tm] = dp[str] + 1;
            }
        }
        if(t<7 && t!=3){
            string tm = str;
            swap(tm[t],tm[t+1]);
            if(dp.find(tm) == dp.end()){
                bfs.push(tm);
                dp[tm] = dp[str] + 1;
            }
        }
        if(t>3){
            string tm = str;
            swap(tm[t],tm[t-4]);
            if(dp.find(tm) == dp.end()){
                bfs.push(tm);
                dp[tm] = dp[str] + 1;
            }                        
        }
        if(t<4){
            string tm = str;
            swap(tm[t],tm[t+4]);
            if(dp.find(tm) == dp.end()){
                bfs.push(tm);
                dp[tm] = dp[str] + 1;
            }                        
        }
    }
    
    string pz;
    pz.resize(8);
    while(cin>>pz[0]){
        rep(i,7)cin >> pz[i+1];
        cout << dp[pz] << endl;
    }
}
