// AOJ 0121
#include<iostream>
#include<queue>
#include<set>
using namespace std;

typedef vector<int> sequence;

sequence empty() {
  sequence s;
  return s;
}

int zero_idx(sequence s) {
  int i = 0;
  while (s[i] != 0) i++;
  return i;
}

sequence sswap(sequence s, int i, int j) {
  int temp = s[i];
  s[i] = s[j];
  s[j] = temp;
  return s;
}

sequence move_left(sequence s) {
  int z = zero_idx(s);
  if (z == 0 or z == 4) {
    return empty();
  } else {
    return sswap(s, z, z - 1);
  }
}

sequence move_right(sequence s) {
  int z = zero_idx(s);
  if (z == 3 or z == 7) {
    return empty();
  } else {
    return sswap(s, z, z + 1);
  }
}

sequence move_vertically(sequence s) {
  int z = zero_idx(s);
  return sswap(s, z, (z + 4) % 8); 
}

#define REP(i,n) for(int i=0;i<(int)(n);i++)

bool finished(sequence s) {
  REP(i, 8) {
    if (i != s[i]) return false;
  }
  return true;
}

typedef pair<int,sequence> P;

int solve(sequence s) {
  queue<P> q;
  set<sequence> states;
  q.push(make_pair(0,s));
  while (not q.empty()) {
    P p = q.front(); q.pop();
    int step = p.first;
    sequence current = p.second;

    if (finished(current)) {
      return step;
    }
    sequence l = move_left(current), r = move_right(current),
             v = move_vertically(current);
    if (!l.empty() and states.find(l) == states.end()) {
      q.push(make_pair(step + 1, l));
      states.insert(l);
    }
    if (!r.empty() and states.find(r) == states.end()) {
      q.push(make_pair(step + 1, r));
      states.insert(r);
    }
    if (states.find(v) == states.end()) {
      q.push(make_pair(step + 1, v));
      states.insert(v);
    }
  }
}

int main() {
  sequence s(8);
  while (cin >> s[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> s[i];
    }

    cout << solve(s) << endl;
  }
}