#include <iostream>
#include <string>
#include <sstream>
#include <queue>
#include <map>
#include <cmath>

using namespace std;

const int N = 8;

class card
{
public:
    template<typename T>
    card( T const& t )
    {
        for( int i=0; i<2; ++i ) {
            for( int j=0; j<N/2; ++j ) {
                num_[i][j] = t[i*N/2+j];
                if ( num_[i][j] == 0 ) {
                    x0_ = j;
                    y0_ = i;
                }
            }
        }
    }

public:
    card make_moved( int const dir ) const
    {
        card ret( *this );

        switch( dir )
        {
        case 0:
            ret.to_up();
            break;
        case 1:
            ret.to_right();
            break;
        case 2:
            ret.to_down();
            break;
        case 3:
            ret.to_left();
            break;
        }

        return ret;
    }

public:
    operator double() const
    {
        double p = 0.0;

        for( int i=0; i<2; ++i ) {
            for( int j=0; j<N/2; ++j ) {
                p += static_cast<double>(num_[i][j]) * pow( 10.0, i*N/2+j );
            }
        }

        return p;
    }
    
    bool operator==( card const& rhs ) const
    {
        return static_cast<double>( *this ) == static_cast<double>( rhs );
    }

    bool operator<( card const& rhs ) const
    {
        return static_cast<double>( *this ) < static_cast<double>( rhs );
    }

private:
    void to_down()
    {
        if ( y0_ == 0 ) {
            swap( num_[y0_][x0_], num_[y0_+1][x0_] );
            ++y0_;
        }
    }

    void to_up()
    {
        if ( y0_ == 1 ) {
            swap( num_[y0_][x0_], num_[y0_-1][x0_] );
            --y0_;
        }
    }

    void to_right()
    {
        if ( x0_ != N/2-1 ) {
            swap( num_[y0_][x0_], num_[y0_][x0_+1] );
            ++x0_;
        }
    }

    void to_left()
    {
        if ( x0_ != 0 ) {
            swap( num_[y0_][x0_], num_[y0_][x0_-1] );
            --x0_;
        }
    }

private:
    int num_[2][4];
    int x0_, y0_;
};


int main()
{
    int const goal[N] = {0, 1, 2, 3, 4, 5, 6, 7};

    for(int r=0; r<1000; ++r ) {
        if ( cin.eof() )
            break;

        int nn[N];
        for( int i=0; i<N; ++i ) {
            cin >> nn[i];
        }
        card const start( nn );

        map<card, bool> memo;
        map<card, bool> collected, answers;

        queue<pair<int, card> > q;
        q.push( make_pair( 0, card( goal ) ) );

        int times = 0;

        // loop
        while( !q.empty() ) {
            int const cost = q.front().first;
            card const c = q.front().second;
            q.pop();

            if ( collected[c] ) {
                times = answers[c] + cost;
                break;
            }

            if ( c == start ) {
                collected[c] = true;
                answers[c] = cost;
                times = cost;
                break;
            }

            for( int i=0; i<4; ++i ) {
                card const nc = c.make_moved( i );
                if ( nc == c || memo[nc] )
                    continue;

                memo[nc] = true;
                q.push( make_pair( cost + 1, nc ) );
            }
        }

        cout << times << endl;
    }
}