#include <bits/stdc++.h>

using namespace std;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline int stoi(const string& s)
{
  stringstream ss(s);
  int x;
  ss >> x;
  return x;
}

struct Solver {
  Solver(const string& init) : init(init) {}
  int solve(){
    set<int> vis;
    queue<string> Q;
    vis.insert(stoi(init));
    Q.push(init);

    int turn = 0;
    while (vis.find(1234567) == vis.end()){
      queue<string> nQ;
      while (!Q.empty()){
        string state = Q.front();
        Q.pop();
        for (int i = 0; i < 4; i++){
          string next_state;
          if (!slide(state, i, next_state)) continue;
          int visv = stoi(next_state);
          if (vis.find(visv) != vis.end()) continue;
          vis.insert(visv);
          nQ.push(next_state);
        }
      }
      swap(Q, nQ);
      turn++;
    }
    return turn;
  }
  bool slide(const string& src, int dir, string& dst){
    const int width = 4;
    const int up = 0, down = 1, right = 2, left = 3;
    const int zero_pos = src.find('0');
    dst = src;
    if (dir == up){
      if (zero_pos < width) return false;
      swap(dst[zero_pos], dst[zero_pos - width]);
    }
    else if (dir == down){
      if (zero_pos >= width) return false;
      swap(dst[zero_pos], dst[zero_pos + width]);
    }
    else if (dir == right){
      if (zero_pos == width - 1 || zero_pos == 2 * width - 1) return false;
      swap(dst[zero_pos], dst[zero_pos + 1]);
    }
    else {
      if (zero_pos == 0 || zero_pos == width) return false;
      swap(dst[zero_pos], dst[zero_pos - 1]);
    }
    return true;
  }
  string init;
};

int main()
{
  string s;
  while (getline(cin, s)){
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    Solver solver(s);
    printf("%d\n", solver.solve());
  }

  return 0;
}