#include <iostream>
#include <map>
#include <vector>
#include <queue>

#define ALL(a) (a).begin(). (a).end()

using namespace std;
/*
template<class T>
class queue{
    private:
        T *array;
        int size;
        int m_size;
        int head;
        int rear;
    public:
        queue(){
            size = 0; head = 0; rear = 0;
            m_size = 16;
            array = new T[m_size];
        }
        queue(T data){
            size = 1; head = 0; rear = 1;
            m_size = 16;
            array = new T[m_size];
            array[0] = data;
        }
        ~queue(){
            delete array;
        }
        T front(){
            return array[head];
        }
		bool empty(){
			return size == 0;
		}
        void push(T data){
            if(size == m_size){
                m_size <<= 1;
                T *new_array = new T[m_size];
                for(int i = 0; i < size; i++){
                    new_array[i] = array[(head+i)%size];
                }
                head = 0; rear = size;
				T *temp = array;
				array = new_array;
                delete[] temp;
            }
            rear = (rear+1)%m_size;
            array[(rear+m_size-1)%m_size] = data;
            ++size;
        }
        void pop(){
            if(size == 0){return ;}
            head = (head+1)%m_size;
            --size;
        }
};
*/

int dy[] = { 0, 1, 0,-1};
int dx[] = {-1, 0, 1, 0};

int main(){
	int initial;
    vector<int> g(8);
	for(int i = 0; i < 2; i++)for(int j = 0; j < 4; j++){
		g[i*4+j] = i*4+j;
	}
	while(cin >> initial){
		vector<int> c(8);
		for(int i = 0; i < 2; i++)for(int j = 0; j < 4; j++){
			if(i == 0 && j == 0)c[i*4+j] = initial;
            else cin >> c[i*4+j];
		}
		queue<vector<int> > q;
		q.push(c);
		map<vector<int>, int> m;
		m[c] = 0;
		while(!q.empty()){
			auto t = q.front();
			if(t == g)break;
			q.pop();
			int y,x;
			for(int i = 0; i < 2; i++)for(int j = 0; j < 4; j++){
				if(t[i*4+j] == 0){
					y = i; x = j;
					i = j = 100;
				}
			}
			for(int i = 0; i < 4; i++){
				auto tt = t;
				int ny = y+dy[i], nx = x+dx[i];
				if(ny < 0 || ny > 1 || nx < 0 || nx > 3)continue;
                swap(tt[y*4+x], tt[ny*4+nx]);
				if(m.find(tt) == m.end()){
					m[tt] = m[t]+1;
					q.push(tt);
				}
			}
		}
		cout << m[g] << endl;
	}
	return 0;
}

