#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

vector<string> split(const string& str, const string& seps) {
  char *p, *str_buff = new char[str.size() + 1];
  vector<string> res;
  strcpy(str_buff, str.c_str());
  p = strtok(str_buff, seps.c_str());
  while (p) {
    res.emplace_back(p);
    p = strtok(NULL, seps.c_str());
  }
  delete str_buff;
  return res;
}

struct Puzzle {
  int f[8];
  int space;
  Puzzle() {}
  bool check() {
    for (int i = 0; i < 8; i++) {
      if (f[i] != i) return false;
    }
    return true;
  }
  bool operator < (const Puzzle& p) const {
    for (int i = 0; i < 8; i++) {
      if (f[i] == p.f[i]) continue;
      return f[i] < p.f[i];
    }
    return false;
  }
};

int dy[] = {-1, 0, 0, 1};
int dx[] = {0, -1, 1, 0};
map<Puzzle, int> dis;

void init() {
  Puzzle p;
  iota(p.f, p.f + 8, 0);
  p.space = 0;
  queue<Puzzle> que;
  que.emplace(p);
  dis[p] = 0;
  while (!que.empty()) {
    Puzzle u = que.front(); que.pop();
    int y = u.space / 4, x = u.space % 4;
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < 2 && nx >= 0 && nx < 4) {
        Puzzle v = u;
        swap(v.f[y * 4 + x], v.f[ny * 4 + nx]);
        v.space = ny * 4 + nx;
        if (dis.find(v) != dis.end()) continue;
        dis[v] = dis[u] + 1;
        que.emplace(v);
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  init();
  string s;
  while (getline(cin, s)) {
    auto v = split(s, " ");
    Puzzle p2;
    for (int i = 0; i < 8; i++) {
      p2.f[i] = stoi(v[i]);
      if (p2.f[i] == 0) p2.space = i;
    }
    cout << dis[p2] << endl;
  }

  return 0;
}
