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

typedef long long int integer;
typedef integer sequence;

integer pow(integer a, integer n) {
  if (n == 0) {
    return 1;
  } else if (n % 2) {
    integer rest = pow(a, n - 1);
    return rest * a;
  } else {
    integer sqr = pow(a, n / 2);
    return sqr * sqr;
  }
}

integer at(integer i, sequence s) {
  if (i == 0) {
    return s % 10;
  } else {
    return at(i - 1, s / 10);
  }
}

void setat(integer i, integer x, sequence &s) {
  integer now = at(i, s);
  s -= now * pow(10, i);
  s += x * pow(10, i);
}

sequence empty() {
  return 0;
}

integer zero_idx(sequence s) {
  integer i = 0;
  while (at(i, s) != 0) i++;
  return i;
}

sequence sswap(sequence s, integer i, integer j) {
  integer temp = at(i, s);
  setat(i, at(j, s), s);
  setat(j, temp, s);
  return s;
}

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

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

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

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

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

typedef pair<integer,sequence> P;

integer 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();
    integer 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 != 0 and states.find(l) == states.end()) {
      q.push(make_pair(step + 1, l));
      states.insert(l);
    }
    if (r != 0 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;
  integer panel[8];
  while (cin >> panel[0]) {
    for (integer i = 1; i < 8; i++) {
      cin >> panel[i];
    }
    REP(i, 8) {
      setat(i, panel[i], s);
    }

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