/*
  1. don't do bfs each sample
  2. note when 0 is in the 3 and 4 position.
 */

#include <iostream>
#include <string>
#include <unordered_map>
#include <queue>
#include <algorithm>
using namespace std;

unordered_map<int, int> res;
int dpos[] = {1, -1, 4, -4};

void bfs() {
  int init = 1234567;
  res[init] = 0;
  queue<int> qu;
  qu.push(init);
  while (!qu.empty()) {
    int curr = qu.front(); qu.pop();
    int curr_pos = res[curr];
    string curr_s = to_string(curr);
    int pos0 = 0;
    for (int i = 0; i < curr_s.size(); ++i) 
      if (curr_s[i] == '0') {
        pos0 = i;
        break;
      }
    if (pos0 == 0) 
      curr_s = "0" + curr_s;
    for (int i = 0; i < 4; ++i) {
      if (pos0 == 3 && dpos[i] == 1 || pos0 == 4 && dpos[i] == -1) 
        continue;
      int npos = pos0 + dpos[i];
      if (npos >= 0 && npos < 8) {
        swap(curr_s[pos0], curr_s[npos]);
        int new_d = stoi(curr_s);
        if (res.find(new_d) == res.end()) {
          res[new_d] = curr_pos + 1;
          qu.push(new_d);
        }
        swap(curr_s[pos0], curr_s[npos]);
      }
    }
  }
  return;
}

int main() {
  bfs();
  string line;
  while (getline(cin, line)) {
    // remove() in algorithm
    line.erase(remove(line.begin(), line.end(), ' '), line.end());
    int input_digit = stoi(line);
    cout << res[input_digit] << endl;
  }
  return 0;
}
