#include <bits/stdc++.h>                                                                                                   
                                                                                                                           
using namespace std;                                                                                                       
                                                                                                                           
struct Card {                                                                                                              
    int x, y, cnt;                                                                                                         
    string stat[2];                                                                                                        
};                                                                                                                         
                                                                                                                           
int main() {                                                                                                               
                                                                                                                           
    int vx[] = {0, 1, 0, -1};                                                                                              
    int vy[] = {-1, 0, 1, 0};                                                                                              
                                                                                                                           
    while(1) {                                                                                                             
                                                                                                                           
        char num;                                                                                                          
        map<string, bool> check;                                                                                           
        queue<Card> q;                                                                                                     
        Card start;                                                                                                        
                                                                                                                           
        start.stat[0] = "";                                                                                                
        start.stat[1] = "";                                                                                                
                                                                                                                           
        for(int i = 0; i < 2; ++i) {                                                                                       
            for(int j = 0; j < 4; ++j) {                                                                                   
                if(!(cin >> num)) {                                                                                        
                    return 0;                                                                                              
                } else if(num == '0') {                                                                                    
                    start.x = j;                                                                                           
                    start.y = i;                                                                                           
                }                                                                                                          
                start.stat[i] += num;                                                                                      
            }                                                                                                              
        }                                                                                                                  
                                                                                                                           
        start.cnt = 0;                                                                                                     
        q.push(start);                                                                                                     
                                                                                                                           
        while(!q.empty()) {                                                                                                
                                                                                                                           
            Card now = q.front(); q.pop();                                                                                 
                                                                                                                           
            if(now.stat[0] + now.stat[1] == "01234567") {                                                                  
                cout << now.cnt << endl;                                                                                   
                break;                                                                                                     
            }                                                                                                              
                                                                                                                           
            for(int i = 0; i < 4; ++i) {                                                                                   
                                                                                                                           
                Card next;                                                                                                 
                next.x = now.x + vx[i];                                                                                    
                next.y = now.y + vy[i];                                                                                    
                next.cnt = now.cnt + 1;                                                                                    
                next.stat[0] = now.stat[0];                                                                                
                next.stat[1] = now.stat[1];                                                                                
                                                                                                                           
                if(next.x >= 0 && next.x < 4 && next.y >= 0 && next.y < 2) {                                               
                    swap(next.stat[now.y][now.x], next.stat[next.y][next.x]);                                              
                    if(!check[next.stat[0] + next.stat[1]]) {                                                              
                        check[next.stat[0] + next.stat[1]] = true;                                                         
                        q.push(next);                                                                                      
                    }                                                                                                      
                }                                                                                                          
                                                                                                                           
            }                                                                                                              
                                                                                                                           
        }                                                                                                                  
                                                                                                                           
    }                                                                                                                      
                                                                                                                           
}                                                                                                                          