#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int solve(vector<int>& cards) {
  vector<int> goal(8);
  REP(i, 8) goal[i] = i;

  queue<pair<int, VI> > q;
  set<VI> s;
  q.push(make_pair(0, cards));
  while (!q.empty()) {
    pair<int, VI> p = q.front(); q.pop();
    if (p.second == goal) return p.first;

    if (s.find(p.second) != s.end()) continue;
    s.insert(p.second);
    /*
    cerr << "--(" << p.first << ")--" << endl;
    REP(i, 4) cerr << p.second[i]; cerr << endl;
    REP(i, 4) cerr << p.second[i+4]; cerr << endl;
    */

    int pos0 = 0;
    REP(i, 8) if (p.second[i] == 0) pos0 = i;
    if (pos0 % 4 > 0) {
      vector<int> vn = p.second;
      swap(vn[pos0], vn[pos0 - 1]);
      q.push(make_pair(p.first+1, vn));
    }
    if (pos0 % 4 < 3) {
      vector<int> vn = p.second;
      swap(vn[pos0], vn[pos0 + 1]);
      q.push(make_pair(p.first+1, vn));
    }
    if (pos0 / 4 == 0) {
      vector<int> vn = p.second;
      swap(vn[pos0], vn[pos0 + 4]);
      q.push(make_pair(p.first+1, vn));
    } else {
      vector<int> vn = p.second;
      swap(vn[pos0], vn[pos0 - 4]);
      q.push(make_pair(p.first+1, vn));
    }
  }
  return -1;
}

int main() {
  string s;
  while (getline(cin, s)) {
    stringstream sin(s);
    vector<int> cards(8);
    REP(i, 8) sin >> cards[i];
    int answer = solve(cards);
    cout << answer << endl;
  }
}