alexjercan/bugnet huggingface dataset:
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<string, int> dp;
int dir[4] = {-4, 4, -1, 1}; // 上下左右
void bfs() {
  queue<string> que;
  dp["01234567"] = 0;
  que.push("01234567");

  while (!que.empty()) {
    string now = que.front();
    que.pop();
    int p = 0;
    for (int i = 0; i < 8; i++) {
      if (now[i] == '0') {
        p = i;
        break;
      }
    }

    for (int i = 0; i < 4; i++) {
      int t = p + dir[i];
      if (t <= 7 && t >= 0 && !(p == 3 && i == 3) && !(p == 4 && i == 2)) {
        string next = now;
        swap(next[p], next[t]);
        if (dp.find(next) == dp.end()) {
          que.push(next);
          dp[next] = dp[now] + 1;
        }
      }
    }
  }
}
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  bfs();
  string line;
  while (getline(cin, line)) {
    line.erase(remove(line.begin(), line.end(), ' '), line.end());
    cout << dp[line] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <memory.h>
#include <queue>
using namespace std;
struct node {
  int a[8];
  int pos, cnt;
};
const int fac[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
const int mov[4] = {1, -1, 4, -4};
int num[8], fpos;
int goal = 0;
bool vis[40320];

// X = a_n * (n-1)! + a_n-1 * (n-2)! + ... + a_1 * (0)!
int cantor(int *a) {
  int ans = 0;
  for (int i = 0; i < 8; i++) {
    int cnt = 0;
    for (int j = i + 1; j < 8; j++) {
      if (a[j] < a[i])
        cnt++;
    }
    ans += cnt * fac[7 - i];
  }
  return ans;
}

void Swap(int &a, int &b) {
  int t = a;
  a = b;
  b = t;
}

void bfs() {
  queue<node> que;
  node temp;
  memset(vis, false, sizeof(vis));
  memcpy(temp.a, num, sizeof(num));
  temp.pos = fpos;
  temp.cnt = 0;
  que.push(temp);
  while (!que.empty()) {
    temp = que.front();
    que.pop();
    int c = cantor(temp.a);
    if (c == goal) {
      printf("%d\\n", temp.cnt);
      break;
    }
    vis[c] = true;
    for (int i = 0; i < 4; i++) {
      memcpy(num, temp.a, sizeof(temp.a));
      int tempos = temp.pos + mov[i];
      if (tempos < 0 || tempos > 7 || (temp.pos == 3 && tempos == 4) ||
          (temp.pos == 4 && tempos == 3))
        continue;
      Swap(num[temp.pos], num[tempos]);
      if (vis[cantor(num)])
        continue;
      node nd;
      memcpy(nd.a, num, sizeof(num));
      nd.pos = tempos;
      nd.cnt = temp.cnt + 1;
      que.push(nd);
    }
  }
  // printf("error\\n");
}

void solve() { bfs(); }

int main() {
  // freopen("in.txt", "r", stdin);
  while (~scanf("%d", &num[0])) {
    for (int i = 1; i < 8; i++) {
      scanf("%d", &num[i]);
      if (num[i] == 0)
        fpos = i;
    }
    solve();
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define sd(x) scanf("%d", &x)
#define ss(x) scanf("%s", x)
#define sc(x) scanf("%c", &x)
#define sf(x) scanf("%f", &x)
#define slf(x) scanf("%lf", &x)
#define slld(x) scanf("%lld", &x)
#define me(x, b) memset(x, b, sizeof(x))
#define pd(d) printf("%d\\n", d);
#define plld(d) printf("%lld\\n", d);
// #define Reast1nPeace

typedef long long ll;

using namespace std;

const int INF = 0x3f3f3f3f;

map<string, int> m;

int dir[4] = {-4, 4, -1, 1};

void bfs() {
  queue<string> q;
  m["01234567"] = 1;
  q.push("01234567");

  while (!q.empty()) {
    string t = q.front();
    q.pop();

    int pi;
    for (int i = 0; i < 8; i++) {
      if (t[i] == '0') {
        pi = i;
        break;
      }
    }

    for (int i = 0; i < 4; i++) {
      if (pi == 4 && i == 2)
        continue;
      if (pi == 3 && i == 3)
        continue;

      int np = pi + dir[i];
      if (np < 0 || np >= 8)
        continue;

      string st = t;
      swap(st[pi], st[np]);
      if (m[st] == 0) {
        m[st] = m[t] + 1;
        q.push(st);
      }
    }
  }
}

int main() {
#ifdef Reast1nPeace
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  bfs();
  string s;
  while (getline(cin, s)) {
    string mapp = "";
    for (int i = 0; i < s.size(); i++) {
      if (i % 2 == 0) {
        mapp += s[i];
      }
    }
    cout << m[mapp] - 1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define sd(x) scanf("%d", &x)
#define ss(x) scanf("%s", x)
#define sc(x) scanf("%c", &x)
#define sf(x) scanf("%f", &x)
#define slf(x) scanf("%lf", &x)
#define slld(x) scanf("%lld", &x)
#define me(x, b) memset(x, b, sizeof(x))
#define pd(d) printf("%d\\n", d);
#define plld(d) printf("%lld\\n", d);
// #define Reast1nPeace

typedef long long ll;

using namespace std;

const int INF = 0x3f3f3f3f;

string mapp;

map<string, int> m;

int dir[4] = {-4, 4, -1, 1};

void bfs() {
  queue<string> q;

  q.push("01234567");

  while (!q.empty()) {
    string t = q.front();
    q.pop();

    int pi;
    for (int i = 0; i < 8; i++) {
      if (t[i] == '0') {
        pi = i;
        break;
      }
    }

    for (int i = 0; i < 4; i++) {
      if (pi == 4 && i == 2)
        continue;
      if (pi == 3 && i == 3)
        continue;

      int np = pi + dir[i];
      if (np < 0 || np >= 8)
        continue;

      string st = t;
      swap(st[pi], st[np]);
      if (m.find(st) != m.end())
        continue;

      m.insert(make_pair(st, m[t] + 1));
      q.push(st);
    }
  }
}

int main() {
#ifdef Reast1nPeace
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  m.insert(make_pair("01234567", 0));
  bfs();

  while (1) {
    int n;
    mapp = "";
    for (int i = 0; i < 8; i++) {
      scanf("%d", &n);
      mapp += n + '0';
    }
    cout << m[mapp] << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int a[8];
map<int, int> mp;
int st[8][3] = {{1, 4, -1}, {0, 2, 5}, {1, 3, 6}, {2, 7, -1},
                {0, 5, -1}, {1, 4, 6}, {2, 5, 7}, {3, 6, -1}};

bool game_over(int *state) {
  for (int i = 0; i < 8; ++i) {
    if (state[i] != i)
      return false;
  }
  return true;
}

void to_state(int s, int *state) {
  for (int i = 7; i >= 0; --i) {
    state[i] = s % 10;
    s /= 10;
  }
}

int get_state(int *state) {
  int res = 0;
  for (int i = 0; i < 8; ++i) {
    res = res * 10 + state[i];
  }
  return res;
}
void init() {
  queue<int> q;
  q.push(1234567);
  mp[1234567] = 0;
  int is_found = false;
  int state[8];
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    to_state(now, state);
    int zero_pos = find(state, state + 8, 0) - state;
    for (int i = 0; i < 3; ++i) {
      if (st[zero_pos][i] != -1) {
        swap(state[st[zero_pos][i]], state[zero_pos]);
        int ns = get_state(state);
        if (mp.count(ns) == 0) {
          mp[ns] = mp[now] + 1;
          q.push(ns);
        }
        swap(state[st[zero_pos][i]], state[zero_pos]);
      }
    }
  }
}
int main() {
  init();
  while (true) {
    for (int i = 0; i < 8; ++i) {
      if (scanf("%d", a + i) == EOF)
        goto END;
    }
    int start = get_state(a);
    printf("%d\\n", mp[start]);
  }
END:
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int ANSWER = 1234567;
typedef pair<int, int> P;
vector<int> st[ANSWER];
int arr[8], check;
int fx[] = {-1, 1, -4, 4};
bool try_insert(int k) {
  int h = k % ANSWER;
  for (int i = 0; i < st[h].size(); i++)
    if (st[h][i] == k)
      return true;
  st[h].push_back(k);
  return false;
}
int find() {
  for (int i = 0; i < 8; i++)
    if (!arr[i])
      return i;
}
int id() {
  int ans = 0;
  for (int i = 0; i < 8; i++)
    ans = ans * 10 + arr[i];
  return ans;
}
void translate(int ans) {
  for (int i = 7; i >= 0; i--)
    arr[i] = ans % 10, ans /= 10;
}
void bfs(int s) {
  queue<P> que;
  que.push(P(s, 1));
  st[s % ANSWER].push_back(s);
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    translate(p.first);
    int old[8];
    memcpy(old, arr, sizeof(arr));
    int b = find();
    for (int i = 0; i < 4; i++) {
      memcpy(arr, old, sizeof(arr));
      int a = b + fx[i];
      if ((a == 4 && i == 0) || (a == 3 && i == 1))
        continue;
      if (a < 0 || a >= 8)
        continue;
      arr[b] += arr[a];
      arr[a] = arr[b] - arr[a];
      arr[b] -= arr[a];
      int k = id();
      if (try_insert(k))
        continue;
      if (k == ANSWER) {
        printf("%d\\n", p.second);
        return;
      }
      que.push(P(k, p.second + 1));
    }
  }
}
int main() {
  while (scanf("%d %d %d %d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3],
               &arr[4], &arr[5], &arr[6], &arr[7]) == 8) {
    for (int i = 0; i < ANSWER; i++)
      st[i].clear();
    int k = id();
    if (k == ANSWER)
      printf("0\\n");
    bfs(k);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 304;

int dir[] = {-4, 4, -1, 1}; // 上下左右
map<string, int> table;
void bfs(string st) {
  queue<string> q;
  table[st] = 1; // 起始状态
  q.push(st);
  while (!q.empty()) {
    st = q.front();
    q.pop();
    int num = st.find('0');
    for (int i = 0; i < 4; i++) {
      int tn = num + dir[i];
      if (tn < 0 || tn > 7 || (num == 3 && dir[i] == 1) ||
          (num == 4 && dir[i] == -1))
        continue;
      string tp = st;
      swap(tp[num], tp[num + dir[i]]);
      if (!table[tp]) {
        q.push(tp);
        table[tp] = table[st] + 1;
      }
    }
  }
}

int main() {
  int num;
  string st = "01234567";
  bfs(st);
  while (~scanf("%d", &num)) {
    st[0] = num + '0';

    for (int i = 1; i < 8; i++) {
      scanf("%d", &num);
      st[i] = num + '0';
    }
    printf("%d\\n", table[st] - 1);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int main() {
  string raw;
  //    string ans = "01234567";
  while (getline(cin, raw)) {
    if (raw == "")
      continue;
    string ans;
    for (auto c : raw) {
      if (c == ' ')
        continue;
      ans.push_back(c);
    }
    string init = "01234567";
    queue<pair<string, int>> Q;
    unordered_map<string, bool> visited;
    Q.push({init, 0});
    visited[init] = true;
    bool is_exit = false;
    while ((not Q.empty()) and (not is_exit)) {
      string cur;
      int step;
      tie(cur, step) = Q.front();
      Q.pop();
      //            cout << cur << endl;
      if (cur == ans) {
        cout << step << endl;
        is_exit = true;
      }
      int id = cur.find('0');
      int x = id / 4, y = id % 4;
      for (auto k : d) {
        int nx = k[0] + x;
        int ny = k[1] + y;
        if (nx >= 0 and nx < 2 and ny >= 0 and ny < 4) {
          int nid = nx * 4 + ny;
          string s = cur;
          swap(s[id], s[nid]);
          if (not visited[s]) {
            visited[s] = true;
            Q.push({s, step + 1});
          }
        }
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
string str, str1;
int dx[4] = {-1, 1, 4, -4};
map<string, int> mp;

int posi;
void bfs() {
  queue<string> que;
  que.push("01234567");
  mp["01234567"] = 0;
  while (que.size()) {
    string preq = que.front();
    que.pop();
    for (int i = 0; i < 8; i++)
      if (preq[i] == '0') {
        posi = i;
        break;
      }
    for (int i = 0; i < 4; i++) {
      int nposi = posi + dx[i];
      string q = preq;
      swap(q[posi], q[nposi]);
      if (mp.find(q) != mp.end() || i == 0 && nposi == 3 ||
          i == 1 && nposi == 4 || nposi > 7 || nposi < 0)
        continue;
      que.push(q);
      mp[q] = mp[preq] + 1;
    }
  }
}

int main() {
  bfs();
  while (getline(cin, str1)) {
    str.clear();
    for (int i = 0; i < str1.size(); i++) {
      if (str1[i] != ' ')
        str += str1[i];
    }
    printf("%d\\n", mp[str]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <string>
using namespace std;
string str;
int dx[4] = {-1, 1, 4, -4};
map<string, int> mp;

int posi;
void bfs() {
  queue<string> que;
  que.push("01234567");
  mp["01234567"] = 0;
  while (que.size()) {
    string preq = que.front();
    que.pop();
    for (int i = 0; i < 8; i++)
      if (preq[i] == '0')
        posi = i;
    for (int i = 0; i < 4; i++) {
      int nposi = posi + dx[i];
      if (nposi > 7 || nposi < 0)
        continue;
      if (i == 0 && nposi == 3 || i == 1 && nposi == 4)
        continue;
      string q = preq;
      swap(q[posi], q[nposi]);
      if (mp.find(q) != mp.end())
        continue;
      que.push(q);
      mp[q] = mp[preq] + 1;
    }
  }
}

int main() {
  bfs();
  while (true) {
    int a;
    str = "";
    for (int i = 0; i < 8; i++) {
      scanf("%d", &a);
      str += a + '0';
    }
    printf("%d\\n", mp[str]);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  map<vector<int>, int> MVII;
  vector<int> D(8);
  for (int i = 0; i < 8; i++) {
    D[i] = i;
  }

  queue<vector<int>> que;
  que.push(D);
  while (que.size()) {
    vector<int> now = que.front();
    que.pop();
    int zero;
    for (int i = 0; i < 8; i++) {
      if (now[i] == 0)
        zero = i;
    }
    if (zero >= 4) {
      auto E = now;
      swap(E[zero], E[zero - 4]);
      if (MVII[E] == 0) {
        MVII[E] = MVII[now] + 1;
        que.push(E);
      }
    } else {
      auto E = now;
      swap(E[zero], E[zero + 4]);
      if (MVII[E] == 0) {
        MVII[E] = MVII[now] + 1;
        que.push(E);
      }
    }
    if (zero != 0 && zero != 4) {
      auto E = now;
      swap(E[zero], E[zero - 1]);
      if (MVII[E] == 0) {
        MVII[E] = MVII[now] + 1;
        que.push(E);
      }
    }
    if (zero != 3 && zero != 7) {
      auto E = now;
      swap(E[zero], E[zero + 1]);
      if (MVII[E] == 0) {
        MVII[E] = MVII[now] + 1;
        que.push(E);
      }
    }
  }
  for (int i = 0; i < 8; i++) {
    D[i] = i;
  }
  MVII[D] = 0;
  while (cin >> D[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> D[i];
    }
    cout << MVII[D] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  vector<int> D(8);
  while (cin >> D[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> D[i];
    }
    map<vector<int>, int> M;
    queue<pair<vector<int>, int>> que;
    que.push(make_pair(D, 0));
    auto ans = D;
    sort(ans.begin(), ans.end());
    M[D] = 1;
    while (que.size()) {
      auto now = que.front();
      que.pop();
      if (now.first == ans) {
        cout << now.second << endl;
        break;
      }
      int zero;
      for (int i = 0; i < 8; i++) {
        if (now.first[i] == 0) {
          zero = i;
        }
      }
      auto E = now.first;
      if (zero >= 4) {
        swap(E[zero], E[zero - 4]);
      } else {
        swap(E[zero], E[zero + 4]);
      }
      if (M[E] != 1) {
        que.push(make_pair(E, now.second + 1));
        M[E] = 1;
      }
      if (zero - 1 != -1 && zero - 1 != 3) {
        E = now.first;
        swap(E[zero], E[zero - 1]);
        if (M[E] != 1) {
          que.push(make_pair(E, now.second + 1));
          M[E] = 1;
        }
      }
      if (zero + 1 != 4 && zero + 1 != 8) {
        E = now.first;
        swap(E[zero], E[zero + 1]);
        if (M[E] != 1) {
          que.push(make_pair(E, now.second + 1));
          M[E] = 1;
        }
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <ostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000000
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rev(i, a, b) for (int i = (a)-1; i >= b; i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef queue<pair<int, int>> qpii;
typedef vector<int> vi;
typedef vector<int, int> vii;
typedef vector<string> vs;
typedef vector<pair<int, int>> vpii;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

map<string, int> d;

void bfs() {
  int dn[4] = {4, -4, -1, 1};
  queue<string> qs;

  qs.push("01234567");
  d["01234567"] = 0;

  while (qs.size()) {
    string s = qs.front();
    qs.pop();
    int n = s.find("0");
    rep(i, 0, 4) {
      int ne = n + dn[i];
      if (0 <= ne && ne < 8) {
        if ((n == 4 && ne == 3) || (n == 3 && ne == 4))
          continue;
        string ss = s;

        char tmp = ss[n];
        ss[n] = ss[ne];
        ss[ne] = tmp;

        if (d.find(ss) == d.end()) {
          qs.push(ss);
          d[ss] = d[s] + 1;
        }
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  bfs();

  while (1) {
    string in = "";
    char num;
    rep(i, 0, 8) {
      if (cin >> num)
        in += num;
      else
        return 0;
    }
    cout << d[in] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <ostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000000
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rev(i, a, b) for (int i = (a)-1; i >= b; i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef queue<pair<int, int>> qpii;
typedef vector<int> vi;
typedef vector<int, int> vii;
typedef vector<string> vs;
typedef vector<pair<int, int>> vpii;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

map<string, int> d;

void bfs() {
  int dn[4] = {4, -4, -1, 1};
  queue<string> qs;

  qs.push("01234567");
  d["01234567"] = 0;

  while (qs.size()) {
    string s = qs.front();
    qs.pop();
    int n = s.find("0");
    rep(i, 0, 4) {
      int ne = n + dn[i];
      if (0 <= ne && ne < 8) {
        if ((n == 4 && ne == 3) || (n == 3 && ne == 4))
          continue;
        string ss = s;

        char tmp = ss[n];
        ss[n] = ss[ne];
        ss[ne] = tmp;

        if (d.find(ss) == d.end()) {
          qs.push(ss);
          d[ss] = d[s] + 1;
        }
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  bfs();

  while (1) {
    string in = "";
    char num;
    rep(i, 0, 8) {
      if (cin >> num)
        in += num;
      else
        break;
    }
    cout << d[in] << endl;
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<string, int> dp;
int direction[4] = {1, -1, 4, -4};

//************************************
// Method:    bfs
// FullName:  bfs
// Access:    public
// Returns:   void
// Qualifier: ???0????????´?????????
//************************************
void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string now = que.front();
    que.pop();
    // p???'0'?????????
    int p = 0;
    for (int j = 0; j < 8; ++j) {
      if (now[j] == '0') {
        p = j;
        break;
      }
    }

    for (int i = 0; i < 4; ++i) {
      int n = p + direction[i];
      if (0 <= n && n < 8 &&
          !(p == 3 && i == 0) && // ???????§???????????????????
          !(p == 4 && i == 1))   // ???????§???????????????????
      {
        string next = now;
        swap(next[p], next[n]);
        if (dp.find(next) == dp.end()) {
          dp[next] = dp[now] + 1;
          que.push(next);
        }
      }
    }
  }
}

///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[]) {

  bfs();
  string line;
  while (getline(cin, line)) {
    line.erase(remove(line.begin(), line.end(), ' '), line.end());
    cout << dp[line] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

//
// Created by yijiezhu on 12/21/16.
//
#include <iostream>
#include <map>
#include <queue>
#include <set>

using namespace std;

int delta[]{-1, 1, -4, 4};

bool valid(int k) {
  for (int i = 0; i < 8; i++) {
    if ((k >> (i << 2) & 0xF) != i)
      return false;
  }
  return true;
}

void print(int k) {
  for (int i = 0; i < 8; i++) {
    cout << (k >> (i << 2) & 0xF);
    if (i == 3)
      cout << endl;
  }
  cout << endl << endl;
}

int bfs(int k) {
  queue<pair<int, int>> que;
  que.push(make_pair(k, 0));
  map<int, int> visited{{k, 0}};
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int v = p.first;
    // print(v);
    if (valid(v)) {
      /*while (v != 0) {
          print(v);
          v = visited[v];
      }*/
      return p.second;
    }
    int z;
    for (int i = 0; i < 8; i++) {
      if ((v >> (i << 2) & 0xF) == 0)
        z = i;
    }
    for (int q = 0; q < 4; q++) {
      if (z == 4 && delta[q] == -1)
        continue;
      if (z == 3 && delta[q] == 1)
        continue;
      int nz = z + delta[q];
      if (nz < 0 || nz >= 8)
        continue;
      int nk = 0;
      for (int j = 0; j < 8; j++) {
        int t = v >> (j << 2) & 0xF;
        if (j == z)
          nk |= t << (nz << 2);
        else if (j == nz)
          nk |= t << (z << 2);
        else
          nk |= t << (j << 2);
      }
      if (visited.find(nk) == visited.end()) {
        que.push(make_pair(nk, p.second + 1));
        visited[nk] = v;
      }
    }
  }
  return -1;
}

int main() {
  while (!cin.eof()) {
    int k = 0;
    for (int i = 0; i < 8; i++) {
      int t;
      cin >> t;
      k |= t << (i << 2);
    }
    cout << bfs(k) << endl;
  }
}