#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int const h = 2, w = 4, n = 8;

struct puzzle
{
    int f[n];
    int space;

    bool operator<(const puzzle &p) const
    {
        rep(i, n)
        {
            if (f[i] == p.f[i])
                continue;
            return f[i] > p.f[i];
        }
        return 0;
    }
};

map<puzzle, int> mp;
queue<puzzle> q;
pair<int, int> d[4] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

void bfs(puzzle s)
{
    mp[s] = 0;
    q.push(s);

    while (!q.empty())
    {
        puzzle u = q.front();
        q.pop();
        int x = u.space / w, y = u.space % w;

        rep(i, 4)
        {
            int nx = x + d[i].first, ny = y + d[i].second;

            if (!((0 <= nx && nx < h) && (0 <= ny && ny < w)))
                continue;

            puzzle u_copy = u;
            swap(u_copy.f[u.space], u_copy.f[w * nx + ny]);
            u_copy.space = w * nx + ny;

            if (!mp[u_copy])
            {
                mp[u_copy] = mp[u] + 1;
                q.push(u_copy);
            }
        }
    }
}

int main()
{
    puzzle in;
    rep(i, n) in.f[i] = i;
    in.space = 0;
    bfs(in);

    while (1)
    {
        puzzle ans;
        rep(i, n)
        {
            cin >> ans.f[i];
            if (ans.f[i] == 0)
                ans.space = i;
        }
        if (cin.eof())
            break;

        OP(mp[ans]);
    }

    return 0;
}
