/*
AOJ 0121 Seven Puzzle

?¢???????7??°???????¢??????¨2??4????£????????????????7????£??????????????£?????????????1???7???????????°?????????????£???????????????°???????????????
?£??????????????????????????????¨0??¨??????????????????????????????????????????????£?????????\?§???°??????????????\?£???????????????????????????????
????????????????§???¶?????????????????\??¬?§???°????????¶???????????????????§????????§???¶?????¬????????????????????¶???????§???¨?£??????\??°????°?????§???¨??\??????

?????\??????????????\?????????8?????°?????¨???????§???¶??????????????°????¬¬??????????????°?????????????????°????¬¬??????????????°??????

?????????????°???????????°???\?????\????????\??¶?????°???????????¶??????0 1 2 3 4 5 6 7???
 */
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <algorithm>
#include <complex>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define SZ(a) (int)(a).size()
#define FOR(i,a,b) for (int i=(a); i<=(b); ++i)
#define REP(i,n) for (int i=0; i<(n); ++i)
#define ALL(c) c.begin(), c.end()
#define CLR(c,n) memset(c, n, sizeof(c))
#define TR(it, c) for(typeof(c.begin()) it = c.begin();it != c.end(); ++it)
#define CONTAIN(it, c) (c.find(it) != c.end())
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> void checkmin(T &a, T b) { if (b<a) a=b; }
template <class T> void checkmax(T &a, T b) { if (b>a) a=b; }

const int N = 8;
int f[N+1], v[N];
int *dp;

int encode(void) {
  int val[N], loc[N];
  REP(i, N) val[i] = v[i], loc[v[i]] = i;
  int ans = 0;
  REP(i, N) {
    int j = loc[i];
    ans += (j - i) * f[N - 1 - i];
    swap(val[i], val[j]);
    loc[i] = i;
    loc[val[j]] = j;
  }
  return ans;
}

int decode(int idx) {
  REP(i, N) v[i] = i;
  FOR(i, 1, N) {
    int loc = idx % i + (N - i);
    idx /= i;
    swap(v[loc], v[N-i]);
  }
  REP(i, N) if (v[i] == 0) return i;
  return -1;
}

int change[3][8] = {
  {-1, 0, 1, 2, -1, 4, 5, 6},
  {1, 2, 3, -1, 5, 6, 7, -1},
  {4, 5, 6, 7, 0, 1, 2, 3}};

void init(void) {
  f[0] = 1;
  FOR(i, 1, N) f[i] = f[i-1] * i;
  dp = new int[f[N]];
  memset(dp, -1, sizeof(dp[0]) * f[N]);
  dp[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int idx = q.front();
    int z = decode(idx);
    q.pop();
    REP(i, 3) {
      int j = change[i][z];
      if (j == -1) continue;
      swap(v[z], v[j]);
      int next = encode();
      if (dp[next] == -1) {
        dp[next] = dp[idx] + 1;
        q.push(next);
      }
      swap(v[z], v[j]);
    }
  }
}

bool readline(void) {
  REP(i, 8) if (scanf("%d", &v[i]) != 1) return false;
  return true;
}

int main(int argc, char *argv[]) {
  init();
  while (readline()) printf("%d\n", dp[encode()]);
  delete[] dp;
  dp = NULL;
}