
#include <algorithm>
#include <iostream>
#include <string.h>
#include <unordered_set>
#include <queue>

using namespace std;

#define int_ int64_t
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define lb long double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

ll a[8];
struct node {
    string str;
    ll val;
};

int main()
{
    while (~scanf("%lld",a)) {
        string s=to_string(a[0]);
        for (ll i=1;i<8;i++) {
            scanf("%lld",a+i);
            s+=to_string(a[i]);
        }
        string t="01234567";
        unordered_set<string> vis;
        queue<node> q;
        q.push({s,0});
        while (!q.empty()) {
            node cur=q.front();
            q.pop();
            if (vis.count(cur.str)) continue;
            vis.insert(cur.str);
            if (cur.str==t) {
                printf("%lld\n",cur.val);
                break;
            }
            ll idx=0;
            for (ll i=0;i<8;i++) {
                if (cur.str[i]=='0') {
                    idx=i;
                    break;
                }
            }
            if (idx!=0&&idx!=4) {
                string tmp=cur.str;
                swap(tmp[idx],tmp[idx-1]);
                if (vis.count(tmp)==0) q.push({tmp,cur.val+1});
            }
            if (idx!=3&&idx!=7) {
                string tmp=cur.str;
                swap(tmp[idx],tmp[idx+1]);
                if (vis.count(tmp)==0) q.push({tmp,cur.val+1});
            }
            if (idx<4) {
                string tmp=cur.str;
                swap(tmp[idx],tmp[idx+4]);
                if (vis.count(tmp)==0) q.push({tmp,cur.val+1});
            }
            if (idx>4) {
                string tmp=cur.str;
                swap(tmp[idx],tmp[idx-4]);
                if (vis.count(tmp)==0) q.push({tmp,cur.val+1});
            }
        }
    }
}
