alexjercan/bugnet huggingface dataset:
Problem p00121 (C++) — buggy submission, verdict: Memory 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};

void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string now = que.front();
    que.pop();
    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);
        }
      }
    }
  }
}
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: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
struct P {
  int a[8]; // don't write int a[]; will produce error
  int n;
};
int z[8] = {0, 1, 2, 3, 4, 5, 6, 7};
#define fi first
#define se second
int d[4] = {-4, 4, 1, -1};
bool iseq(int a[]) {
  forn(i, 8) {
    if (a[i] != z[i])
      return false;
  }
  return true;
}
int bfs() {
  queue<P> que;
  P p;
  forn(i, 8) cin >> p.a[i];
  p.n = 0;
  que.push(p);
  while (que.size()) {
    p = que.front();
    que.pop();
    if (iseq(p.a))
      return p.n;
    int position = 0;
    while (p.a[position] != 0)
      position++;
    forn(i, 4) {
      int pos = position + d[i];
      if (pos >= 0 && pos < 8) {
        swap(p.a[pos], p.a[position]);
        p.n++;
        que.push(p);
        p.n--;
        swap(p.a[pos], p.a[position]);
      }
    }
  }
  return -1;
}
int main() {
  while (1) {
    cout << bfs() << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
using namespace std;

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

bool in(int a) {
  if (0 <= a && a <= 7)
    return true;
  return false;
}

int main() {
  char c;
  string s;

  queue<string> q;
  map<string, int> m;
  q.push("01234567");
  m["01234567"] = 0;
  int ans = 0;
  while (!q.empty()) {
    string u = q.front();
    q.pop();

    int index = u.find('0');
    for (int j = 0; j < 4; j++) {
      if ((index == 3 && x[j] == 1) || (index == 4 && x[j] == -1))
        continue;
      if (in(index + x[j])) {
        string t = u;
        swap(t[index], t[index + x[j]]);
        if (m.find(t) == m.end()) {
          m[t] = m[u] + 1;
          q.push(t);
        }
      }
    }
  }

  while (cin >> c) {
    s = c;
    for (int i = 0; i < 7; i++) {
      cin >> c;
      s += c;
    }
    cout << m[s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
using namespace std;

string g = "01234567";
int x[4] = {4, -4, 1, -1};

bool in(int a) {
  if (0 <= a && a <= 7)
    return true;
  return false;
}

int main() {
  char c;
  string s;

  while (cin >> c) {
    s = c;
    for (int i = 0; i < 7; i++) {
      cin >> c;
      s += c;
    }

    queue<string> q;
    map<string, int> m;
    q.push(s);
    m[s] = 0;
    int ans = 0;
    while (!q.empty()) {
      string u = q.front();
      q.pop();
      if (u == g) {
        ans = m[u];
        break;
      }
      int index = u.find('0');
      for (int j = 0; j < 4; j++) {
        if ((index == 3 && x[j] == 1) || (index == 4 && x[j] == -1))
          continue;
        if (in(index + x[j])) {
          string t = u;
          swap(t[index], t[index + x[j]]);
          if (m.find(t) == m.end()) {
            m[t] = m[u] + 1;
            q.push(t);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

int a[8];
int d[4] = {-1, 1, -4, 4};

map<string, int> mymap;

void bfs() {
  queue<string> q;
  q.push("01234567");
  mymap["01234567"] = 0;
  while (!q.empty()) {
    string cur = q.front();
    string nxt;
    q.pop();
    int p = (int)cur.find("0");
    for (int i = 0; i < 4; ++i) {
      int np = p + d[i];
      if (np >= 0 && np < 8 && (p != 3 || d[i] != 1) &&
          (p != 4 || d[i] != -1)) {
        nxt = cur;
        nxt[np] = '0';
        nxt[p] = cur[np];
        if (mymap.find(nxt) == mymap.end()) {
          mymap[nxt] = mymap[cur] + 1;
          q.push(nxt);
        }
      }
    }
  }
}

int main() {
  bfs();
  while (cin >> a[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> a[i];
    }
    string s = "";
    for (int i = 0; i < 8; ++i) {
      s += a[i] + '0';
    }
    if (mymap.find(s) == mymap.end())
      cout << -1 << endl;
    else
      cout << mymap[s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
 * AOJ 0121: Seven Puzzle
 * ?¢???????2???4????????????????????????0???7???????¬??????\\?°?0?????¶??????????????¢???????????°?¢?????±???¢???????°??§???¨??\\??°???
 * ?±???????BFS???Map
 * ???????????¨String??¨????±???¢?????¨Map???????±???¢???????????????????°??????????????????????????±???¢???????´¢????§???¨???????????°???????±???¢????????\\???????????´??°?±???¢????¢???????????????????
 */

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

int a[8];
int d[4] = {-1, 1, -4, 4};
string s = "";
string t = "01234567";

int bfs() {
  if (s == t) {
    return 0;
  }
  queue<string> q;
  q.push(s);
  map<string, int> mymap;
  mymap[s] = 0;
  while (!q.empty()) {
    string cur = q.front();
    string nxt;
    q.pop();
    int p = (int)cur.find("0");
    for (int i = 0; i < 4; ++i) {
      int np = p + d[i];
      if (np >= 0 && np < 8 && (p != 3 || d[i] != 1) &&
          (p != 4 || d[i] != -1)) {
        nxt = cur;
        nxt[np] = '0';
        nxt[p] = cur[np];
        if (mymap.find(nxt) == mymap.end()) {
          mymap[nxt] = mymap[cur] + 1;
          if (nxt == t) {
            return mymap[nxt];
          }
          q.push(nxt);
        }
      }
    }
  }
  return -1;
}

int solve() {
  s = "";
  for (int i = 0; i < 8; ++i) {
    s += a[i] + '0';
  }
  return bfs();
}

int main() {
  while (cin >> a[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> a[i];
    }
    cout << solve() << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define foreach(i, n) for (int i = 0; i < n; ++i)
#define foreach_down(i, n) for (int i = (n)-1; 0 <= i; --i)
#define foreach_from(i, m, n) for (int i = m; i < n; ++i)
typedef vector<int> veci;

#define MAX_PAZZLE 1000
int dirctions[4] = {-4, 4, -1, 1};
bool on_board(int n) { return 0 <= n && n < 8; }
bool movable(int n, int m) {
  return on_board(m) && (n + 1) * (m + 1) != (3 + 1) * (4 + 1);
}
int find_zero(const veci &v) { return find(v.begin(), v.end(), 0) - v.begin(); }

int main() {
  veci pzl = veci(8);
  foreach (i, 8)
    pzl[i] = i;
  int cnt = 0;
  map<veci, int> memo;
  memo[pzl] = cnt;
  queue<veci> que;
  que.push(pzl);
  while (not que.empty()) {
    pzl = que.front();
    que.pop();
    cnt = memo[pzl];
    int zero = find_zero(pzl);
    foreach (d, 4) {
      int nzero = zero + dirctions[d];
      if (movable(zero, nzero)) {
        veci npzl = pzl;
        swap(npzl[zero], npzl[nzero]);
        if (memo.find(npzl) == memo.end()) {
          memo[npzl] = cnt + 1;
          que.push(npzl);
        }
      }
    }
  }
  while (true) {
    foreach (i, 8)
      cin >> pzl[i];
    if (cin.fail())
      break;
    cout << memo[pzl] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;
#define foreach(i, n) for (typeof(n) i = 0; i < (n); ++i)
typedef vector<int> veci;

#define MAX_PAZZLE 1000
int dirctions[4] = {-4, 4, -1, 1};
bool on_board(int n) { return 0 <= n && n < 8; }

struct state {
  veci pzl;
  int zero;
  int cnt;
};

bool solved(const veci &pzl) {
  foreach (i, 8)
    if (pzl[i] != i)
      return false;
  return true;
}
int serialize(const veci &pzl) {
  int n = 0;
  foreach (i, 8)
    n += pzl[i] << (i * 3);
  return n;
}

int main() {
  while (true) {
    state stt;
    stt.pzl = veci(8);
    foreach (i, 8)
      cin >> stt.pzl[i];
    if (cin.fail())
      break;
    if (solved(stt.pzl)) {
      cout << 0 << endl;
      continue;
    }
    stt.zero = find(stt.pzl.begin(), stt.pzl.end(), 0) - stt.pzl.begin();
    stt.cnt = 0;
    queue<state> que = queue<state>();
    que.push(stt);
    set<int> searched = set<int>();

    while (not que.empty()) {
      stt = que.front();
      que.pop();
      int serialized = serialize(stt.pzl);
      if (searched.find(serialized) != searched.end())
        continue;
      else
        searched.insert(serialized);
      foreach (d, 4) {
        state nxt = stt;
        ++nxt.cnt;
        nxt.zero += dirctions[d];
        if (not on_board(nxt.zero) || stt.zero * nxt.zero == 3 * 4) {
          continue;
        }
        nxt.pzl[stt.zero] = stt.pzl[nxt.zero];
        nxt.pzl[nxt.zero] = 0;
        if (solved(nxt.pzl)) {
          cout << nxt.cnt << endl;
          que = queue<state>();
          break;
        } else {
          que.push(nxt);
        }
      }
    }
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

// 40
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int main() {
  queue<pair<vector<int>, int>> que;
  static const int g[] = {0, 1, 2, 3, 4, 5, 6, 7};
  vector<int> ov(g, g + 8);
  que.push(make_pair(ov, 0));
  map<vector<int>, int> m;
  while (!que.empty()) {
    vector<int> cv = que.front().first;
    if (m.find(cv) != m.end()) {
      que.pop();
      continue;
    }
    m[cv] = que.front().second;
    int z = find(cv.begin(), cv.end(), 0) - cv.begin();
    for (int i = 0; i < 4; i++) {
      static const int d[] = {1, 4, -1, -4};
      int nz = z + d[i];
      if (0 <= nz && nz < 8 && !(z * nz == 12 && z + nz == 7)) {
        vector<int> nv = cv;
        swap(nv[z], nv[nz]);
        que.push(make_pair(nv, que.front().second + 1));
      }
    }
    que.pop();
  }
  for (;;) {
    vector<int> v(8);
    for (int i = 0; i < 8; i++) {
      cin >> v[i];
      if (!cin)
        return 0;
    }
    cout << m[v] << endl;
  }
}

Error: TLE

Fixed submission:

// 40
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (;;) {
    vector<int> v(8);
    for (int i = 0; i < 8; i++) {
      cin >> v[i];
      if (!cin)
        return 0;
    }
    queue<pair<vector<int>, int>> que;
    que.push(make_pair(v, 0));
    set<vector<int>> s;
    for (;;) {
      vector<int> cv = que.front().first;
      if (s.find(cv) != s.end()) {
        que.pop();
        continue;
      }
      s.insert(cv);
      static const int g[] = {0, 1, 2, 3, 4, 5, 6, 7};
      if (equal(cv.begin(), cv.end(), g))
        break;
      int z = find(cv.begin(), cv.end(), 0) - cv.begin();
      for (int i = 0; i < 4; i++) {
        static const int d[] = {1, 4, -1, -4};
        int nz = z + d[i];
        if (0 <= nz && nz < 8 && z * nz != 12 && z + nz != 7) {
          vector<int> nv = cv;
          swap(nv[z], nv[nz]);
          que.push(make_pair(nv, que.front().second + 1));
        }
      }
      que.pop();
    }
    cout << que.front().second << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

bool check(int n) { return (0 <= n && n < 8); }

void init(map<string, int> &count) {
  const int d[4] = {1, -1, 4, -4};
  queue<string> Q;
  string t = "01234567";
  Q.push(t);

  while (!Q.empty()) {
    string now = Q.front();
    Q.pop();
    int zero = now.find('0');
    int dist = count[now];

    for (int i = 0; i < 4; i++) {
      int next = zero + d[i];

      if (!check(next) || (zero == 3 && d[i] == 1) || (zero == 4 && d[i] == -1))
        continue;

      swap(now[zero], now[next]);
      // not visit
      if (!count[now]) {
        Q.push(now);
        if (now != t)
          count[now] = dist + 1;
      }
      swap(now[zero], now[next]);
    }
  }
}

int main() {
  char in;
  string puzzle;
  map<string, int> count;

  init(count);

  while (cin >> in) {
    puzzle = in;

    for (int i = 0; i < 7; i++) {
      cin >> in;
      puzzle += in;
    }

    cout << count[puzzle] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

bool check(int n) { return (0 <= n && n < 8); }

int bfs(string puzzle) {
  map<string, int> count;
  string t = "01234567";
  const int d[4] = {1, -1, 4, -4};
  queue<string> Q;
  Q.push(puzzle);

  while (!Q.empty()) {
    string now = Q.front();
    Q.pop();
    int zero = now.find('0');
    int dist = count[now];

    if (now == "01234567")
      break;
    for (int i = 0; i < 4; i++) {
      int next = zero + d[i];

      if (!check(next) || (zero == 3 && d[i] == 1) || (zero == 4 && d[i] == -1))
        continue;

      swap(now[zero], now[next]);
      // not visit
      if (!count[now]) {
        Q.push(now);
        count[now] = dist + 1;
      }
      swap(now[zero], now[next]);
    }
  }

  return count[t];
}

int main() {
  char in;
  string puzzle;

  while (cin >> in) {
    puzzle = in;

    for (int i = 0; i < 7; i++) {
      cin >> in;
      puzzle += in;
    }

    cout << bfs(puzzle) << endl;
  }

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

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

int main() {
  int a[8];
  for (int i = 0; i < 8; i++)
    a[i] = i;
  map<string, int> m;
  do {
    string s;
    for (int i = 0; i < 8; i++)
      s += a[i] + '0';
    m.insert(map<string, int>::value_type(s, -1));
  } while (next_permutation(a, a + 8));

  m["01234567"] = 0;

  queue<string> q;
  q.push("01234567");

  string str;
  int tmp;
  while (q.size()) {
    str = q.front();
    q.pop();
    tmp = m[str];

    int pos;
    for (int i = 0; i < 8; i++) {
      if (str[i] == '0')
        pos = i;
    }

    int c[4] = {-4, -1, 1, 4};
    for (int i = 0; i < 4; i++) {
      if (pos + c[i] < 0 || pos + c[i] > 7 || (pos == 3 && c[i] == 1) ||
          pos == 4 && c[i] == -1)
        continue;
      swap(str[pos], str[pos + c[i]]);
      if (m[str] < 0) {
        // cout << tmp << ":[" << str << "]" << endl;
        q.push(str);
        m[str] = tmp + 1;
      }
      swap(str[pos], str[pos + c[i]]);
    }
  }

  while (cin >> a[0]) {
    for (int i = 1; i < 8; i++)
      cin >> a[i];
    string s;
    for (int i = 0; i < 8; i++)
      s += a[i] + '0';

    cout << m[s] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#define F 16777216
using namespace std;

int check[F];

int code(string x) {
  int k = 1, res = 0;
  for (int i = 0; i < 8; i++) {
    res += (x[i] - '0') * k;
    k *= 8;
  }
  return res;
}

int main() {

  for (int i = 0; i < F; i++)
    check[i] = -1;
  check[code("01234567")] = 0;

  queue<string> q;
  q.push("01234567");

  string str;
  int tmp;
  while (q.size()) {
    str = q.front();
    q.pop();
    tmp = check[code(str)];

    int pos;
    for (int i = 0; i < 8; i++) {
      if (str[i] == '0')
        pos = i;
    }

    int c[4] = {-4, -1, 1, 4};
    for (int i = 0; i < 4; i++) {
      if (pos + c[i] < 0 || pos + c[i] > 7 || (pos == 3 && c[i] == 1) ||
          pos == 4 && c[i] == -1)
        continue;
      swap(str[pos], str[pos + c[i]]);
      if (check[code(str)] < 0) {
        // cout << tmp << ":[" << str << "]" << endl;
        q.push(str);
        check[code(str)] = tmp + 1;
      }
      swap(str[pos], str[pos + c[i]]);
    }
  }

  int a[8];
  while (cin >> a[0]) {
    for (int i = 1; i < 8; i++)
      cin >> a[i];
    string s;
    for (int i = 0; i < 8; i++)
      s += a[i] + '0';

    cout << check[code(s)] << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dx[] = {1, -1, 4, -4};
map<string, int> cache;

void make_cache() {
  queue<string> q;
  q.push("01234567");
  cache["01234567"] = 0;
  while (!q.empty()) {
    string s = q.front();
    q.pop();
    int zero_p = s.find('0');
    for (int i = 0; i < 4; i++) {
      if (zero_p < 4 && i == 3)
        continue;
      if (zero_p >= 4 && i == 2)
        continue;
      if (zero_p % 4 == 0 && i == 1)
        continue;
      if (zero_p % 4 == 3 && i == 0)
        continue;
      string t = s;
      swap(t[zero_p], t[zero_p + dx[i]]);
      if (cache[t] != 0 || t == "01234567")
        continue;
      q.push(t);
      cache[t] = cache[s] + 1;
    }
  }
}

int main() {
  make_cache();
  string str;
  while (getline(cin, str)) {
    str.erase(remove(str.begin(), str.end(), ' '), str.end());
    cout << cache[str] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef pair<string, int> state;

string swap(string s, int count) {
  char a, b;
  int zero_p = s.find('0');
  a = s[zero_p];
  b = s[zero_p + count];
  s[zero_p] = b;
  s[zero_p + count] = a;
  //    cout << s << ' ';
  //    cout << zero_p << endl;
  return s;
}

int dx[] = {1, -1, 4, -4};
int main() {
  string str;
  while (getline(cin, str)) {
    str.erase(remove(str.begin(), str.end(), ' '), str.end());
    state init_state(str, 0);
    queue<state> q;
    q.push(init_state);
    map<string, bool> used;
    used[init_state.first];
    while (!q.empty()) {
      //            cout << q.size() << endl;
      state s = q.front();
      q.pop();
      //            cout << s.first << endl;
      if (s.first == "01234567") {
        cout << s.second << endl;
        break;
      }
      if (used[s.first])
        continue;
      used[s.first] = true;
      int zero_p = s.first.find('0');
      for (int i = 0; i < 4; i++) {
        if (zero_p < 4 && i == 3)
          continue;
        if (zero_p >= 4 && i == 2)
          continue;
        if (zero_p % 4 == 0 && i == 1)
          continue;
        if (zero_p % 4 == 3 && i == 0)
          continue;
        //                cout << s.first << ' ' << zero_p << endl;
        state new_state(swap(s.first, dx[i]), s.second + 1);
        q.push(new_state);
        //                cout << new_state.first << endl;
      }
    }
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

typedef long long ll;

constexpr char INF = 100;
constexpr int MOD = 1e9 + 7;

map<int, int> cnt;
int nxt[] = {1, -1, 4, -4};

int main(void) {
  int i, j, k;
  cnt[1234567] = 0;
  queue<string> q;
  q.push("01234567");
  while (!q.empty()) {
    string s = q.front();
    q.pop();
    REP(i, 8) if (s[i] == '0') REP(k, 4) {
      string t = s;
      j = i + nxt[k];
      if (j < 0 || j >= 8 || i + j == 7)
        continue;
      swap(t[i], t[j]);
      if (!cnt.count(stoi(t)) || cnt[stoi(t)] > cnt[stoi(s)] + 1) {
        cnt[stoi(t)] = cnt[stoi(s)] + 1;
        q.push(t);
      }
    }
  }
  while (true) {
    int i, a, x = 0;
    for (i = 0; i < 8 && cin >> a; i++)
      x = x * 10 + a;
    if (x == 0)
      break;
    cout << cnt[x] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

typedef long long ll;

constexpr char INF = 100;
constexpr int MOD = 1e9 + 7;

char cnt[80000000];
int nxt[] = {1, -1, 4, -4};

int main(void) {
  int i, j, k;
  REP(i, 8e7) cnt[i] = INF;
  cnt[1234567] = 0;
  queue<string> q;
  q.push("01234567");
  while (!q.empty()) {
    string s = q.front();
    q.pop();
    REP(i, 8) if (s[i] == '0') REP(k, 4) {
      string t = s;
      j = i + nxt[k];
      if (j < 0 || j >= 8 || i + j == 7)
        continue;
      swap(t[i], t[j]);
      if (cnt[stoi(t)] > cnt[stoi(s)] + 1) {
        cnt[stoi(t)] = cnt[stoi(s)] + 1;
        q.push(t);
      }
    }
  }
  while (true) {
    int i, a, x = 0;
    for (i = 0; i < 8 && cin >> a; i++)
      x = x * 10 + a;
    if (x == 0)
      break;
    cout << cnt[x] << endl;
  }
  return 0;
}