#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long ll;

struct P {
    string puzzle;
    int pos, count;
};

int main()
{
    char input[8];
    while (cin >> input[0]) {
        string puzzle = "";
        int pos;
        REP(i, 1, 8) {
            cin >> input[i];
        }
        rep(i, 8) {
            puzzle += input[i];
            if (input[i]=='0')
                pos = i;
        }
        queue<P> que;
        vector<string> used;
        used.pb(puzzle);
        que.push({puzzle, pos, 0});
        while(!que.empty()){
            P p = que.front(); que.pop();
            cout << que.size() << endl;
            // goal
            if (p.puzzle == "01234567"){
                cout << p.count << endl;
                break;
            }

            // up down
            string s;
            bool ifpush;
            s = p.puzzle;
            swap(s[p.pos], s[(p.pos+4)%8]);
            ifpush = true;
            for (int i = 0; i < used.size(); i++) {
                ifpush = ifpush && !(s==used[i]);
            }
            if (ifpush) {
                que.push({s, (p.pos+4)%8, p.count+1});
                used.pb(s);
            }
            // right
            if(p.pos % 4 != 3){
                s = p.puzzle;
                swap(s[p.pos], s[p.pos+1]);
                ifpush = true;
                for (int i = 0; i < used.size(); i++) {
                    ifpush = ifpush && !(s==used[i]);
                }
                if (ifpush) {
                    que.push({s, p.pos+1, p.count+1});
                    used.pb(s);
                }
            }
            // left
            if(p.pos % 4 != 0){
                s = p.puzzle;
                swap(s[p.pos], s[p.pos-1]);
                ifpush = true;
                for (int i = 0; i < used.size(); i++) {
                    ifpush = ifpush && !(s==used[i]);
                }
                if (ifpush) {
                    que.push({s, p.pos-1, p.count+1});
                    used.pb(s);
                }
            }
        }
    }
}