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

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
typedef pair<int, int> Pi;
map<string, int> min_cost;

Pi getPos(string s) {
  int white = s.find('0');
  return make_pair(white % 4, white / 4);
}
int getPos(Pi point) { return point.second * 4 + point.first; }

int bfs(string s) {

  queue<string> que;
  const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};

  que.push(s);
  min_cost[s] = 0;

  while (!que.empty()) {

    string p = que.front();
    que.pop();
    Pi white = getPos(p);

    for (int i = 0; i < 4; i++) {
      int ny = white.second + dy[i], nx = white.first + dx[i];
      string next = p;
      if (ny < 0 || ny >= 2 || nx < 0 || nx >= 4)
        continue;
      swap(next[getPos(make_pair(nx, ny))], next[getPos(white)]);
      if (min_cost.find(next) != min_cost.end() ||
          min_cost[next] > min_cost[p] + 1)
        continue;
      min_cost[next] = min_cost[p] + 1;
      que.push(next);
    }
  }

  return 0;
}

int main() {
  bfs("01234567");
  string s;
  s.resize(8);
  while (cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]) {
    cout << min_cost[s] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
typedef pair<int, int> Pi;

Pi getPos(string s) {
  int white = s.find('0');
  return make_pair(white % 4, white / 4);
}
int getPos(Pi point) { return point.second * 4 + point.first; }

int bfs(string s) {

  queue<string> que;
  map<string, int> min_cost;
  const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};

  que.push(s);
  min_cost[s] = 0;
  while (!que.empty()) {
    string p = que.front();
    que.pop();
    if (p == "01234567")
      return min_cost[p];

    Pi white = getPos(p);
    for (int i = 0; i < 4; i++) {
      int ny = white.second + dy[i], nx = white.first + dx[i];
      string next = p;
      if (ny < 0 || ny >= 2 || nx < 0 || nx >= 4)
        continue;
      swap(next[getPos(make_pair(nx, ny))], next[getPos(white)]);
      if (min_cost.find(next) == min_cost.end() ||
          min_cost[p] + 1 > min_cost[next]) {
        min_cost[next] = min_cost[p] + 1;
        que.push(next);
      }
    }
  }
  return 0;
}

int main() {
  string s;
  s.resize(8);

  while (cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]) {
    cout << bfs(s) << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<string, int> P;
int dx[] = {-1, 1, -4, 4};
map<string, int> mp;

void bfs() {
  queue<string> que;

  que.push("01234567");

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

    for (int i = 0; i < 4; i++) {
      next = p;
      if (0 <= pos + dx[i] && pos + dx[i] < 8 && !(pos == 3 && i == 1) &&
          !(pos == 4 && i == 0)) {
        swap(next[pos], next[pos + dx[i]]);
        if (mp[next] == 0) {
          que.push(next);
          mp[next] = mp[p] + 1;
        }
      }
    }
  }
}

int main() {
  string puzzle;
  char c;

  mp["01234567"] = 1;
  bfs();

  while (cin >> c) {
    puzzle = "";
    puzzle += c;
    for (int i = 1; i < 8; i++) {
      cin >> c;
      puzzle += c;
    }
    cout << mp[puzzle] - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<string, int> P;
int dx[] = {-1, 1, -4, 4};
int bfs(string s) {
  queue<P> que;
  map<string, int> used;
  que.push(P(s, 0));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.first == "01234567")
      return p.second;
    if (used[p.first])
      continue;
    used[p.first] = 1;
    string puzzle = p.first, next;
    int pos;
    for (int i = 0; i < 8; i++)
      if (puzzle[i] == '0')
        pos = i;

    for (int i = 0; i < 4; i++) {
      next = puzzle;
      if (0 <= pos + dx[i] && pos + dx[i] < 8 && !(pos == 3 && i == 1) &&
          !(pos == 4 && i == 0)) {
        swap(next[pos], next[pos + dx[i]]);
        que.push(P(next, p.second + 1));
      }
    }
  }
}

int main() {
  string puzzle;
  char c;
  while (cin >> c) {
    puzzle = "";
    puzzle += c;
    for (int i = 1; i < 8; i++) {
      cin >> c;
      puzzle += c;
    }
    int res = bfs(puzzle);
    cout << res << endl;
  }
  return 0;
}
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};

//************************************
// 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: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;

char str[8];
char fin[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int dir[4] = {-1, 1, -4, 4};

struct point {
  char temp[8];
  int num;
  point(char *tt, int nn) {
    strcpy(temp, tt);
    num = nn;
  }
};

bool equal(char *p1, char *p2) {
  for (int i = 0; i < 8; i++) {
    if (p1[i] != p2[i])
      return false;
  }
  return true;
}

int bfs(char *s) {
  queue<point> q;
  q.push(point(s, 0));

  while (q.size()) {
    point cur = q.front();
    q.pop();
    if (equal(cur.temp, fin))
      return cur.num;
    int zx;
    for (int i = 0; i < 8; i++) {
      if (cur.temp[i] == '0') {
        zx = i;
        break;
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = zx + dir[i];
      if (nx >= 0 && nx < 8) {
        char t[8];
        strcpy(t, cur.temp);
        char a = t[zx];
        t[zx] = t[nx];
        t[nx] = a;
        q.push(point(t, cur.num + 1));
      }
    }
  }
}

int main() {
  while (cin >> str) {
    cout << bfs(str) << 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[4] = {1, -1, 4, -4};
map<string, int> res;

void solve(void) {
  queue<string> que;
  que.push("01234567");
  while (!que.empty()) {
    string v = que.front();
    que.pop();
    int pos = 0;
    for (int i = 0; i < 8; i++)
      if (v[i] == '0')
        pos = i;

    for (int i = 0; i < 4; i++) {
      if (0 <= pos + dx[i] && pos + dx[i] < 8 && !(pos == 3 && i == 0) &&
          !(pos == 4 && i == 1)) {
        string u = v;
        swap(u[pos], u[pos + dx[i]]);

        if (res[u] == 0) {
          que.push(u);
          res[u] = res[v] + 1;
        }
      }
    }
  }
}

int main(void) {
  int in;
  res["01234567"] = 1;
  solve();
  while (true) {
    string s;
    for (int i = 0; i < 8; i++) {
      if (!(cin >> in))
        return 0;
      s += in + '0';
    }
    cout << res[s] - 1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

queue<string> q;
map<string, int> m;
const int d[] = {1, -1, 4, -4};

void solve() {
  m["01234567"] = 1;
  q.push("01234567");
  while (!q.empty()) {
    int t = 0;
    string res = q.front();
    q.pop();
    for (int i = 0; i < res.length(); i++)
      if (res[i] == '0')
        t = i;
    for (int i = 0; i < 4; i++) {
      if (0 <= t + d[i] && t + d[i] < 8 && !(t == 3 && i == 0) &&
          !(t == 4 && i == 1)) {
        string u = res;
        swap(u[t], u[t + d[i]]);
        if (m[u] == 0) {
          q.push(u);
          m[u] = m[res] + 1;
        }
      }
    }
  }
}
int main() {
  solve();
  while (1) {
    int k;
    string s;
    for (int i = 0; i < 8; i++) {
      cin >> k;
      s = s + char(k + '0');
    }
    cout << m[s] - 1 << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<double, double> P;
int b[19][19];

int main() {

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

  string s = "01234567";

  queue<string> que;
  map<string, int> ans;
  que.push(s);
  ans[s] = 0;

  while (que.size()) {
    string q = que.front();
    que.pop();
    int x = q.find("0");
    for (int i = 0; i < 4; i++) {
      int next = x + dx[i];
      if (next >= 0 && next <= 7 && !(x == 3 && next == 4) &&
          !(x == 4 && next == 3)) {
        string nextq = q;
        swap(nextq[x], nextq[next]);
        if (ans.find(nextq) == ans.end()) {
          que.push(nextq);
          ans[nextq] = ans[q] + 1;
        }
      }
    }
  }

  string input = "";
  while (getline(cin, input)) {
    input.erase(remove(input.begin(), input.end(), ' '), input.end());
    cout << ans[input] << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<double, double> P;
int b[19][19];

int main() {

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

  string s = "01234567";

  queue<string> que;
  map<string, int> ans;
  que.push(s);
  ans[s] = 0;

  while (que.size()) {
    string q = que.front();
    que.pop();
    int x = q.find("0");
    for (int i = 0; i < 4; i++) {
      int next = x + dx[i];
      if (next >= 0 && next <= 7 && !(x == 3 && next == 4) &&
          !(x == 4 && next == 3)) {
        string nextq = q;
        swap(nextq[x], nextq[next]);
        if (ans.find(nextq) == ans.end()) {
          que.push(nextq);
          ans[nextq] = ans[q] + 1;
        }
      }
    }
  }

  while (1) {
    string input = "";
    char temp;
    for (int i = 0; i < 8; i++) {
      if (cin >> temp)
        input.push_back(temp);
    }
    cout << ans[input] << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> init = {0, 1, 2, 3, 4, 5, 6, 7};
vector<vector<int>> adj = {{1, 4}, {0, 2, 5}, {1, 3, 6}, {2, 7},
                           {0, 5}, {1, 4, 6}, {2, 5, 7}, {3, 6}};

map<vector<int>, int> d;

int solve(vector<int> init, vector<int> goal) {
  if (d.find(goal) != d.end()) {
    return d[goal];
  }
  d.clear();
  d[init] = 0;
  queue<vector<int>> q;
  q.push(init);
  while (!q.empty()) {
    vector<int> v = q.front();
    q.pop();
    int step = d[v];
    if (v == goal)
      return step;
    for (int i = 0; i < 8; ++i) {
      if (v[i] == 0) {
        for (int j = 0; j < (int)adj[i].size(); ++j) {
          swap(v[i], v[adj[i][j]]);
          if (d.find(v) == d.end()) {
            q.push(v);
            d[v] = step + 1;
          }
          swap(v[i], v[adj[i][j]]);
        }
      }
    }
  }
  return -1;
}

int main() {
  vector<int> v(8);
  while (1) {
    for (int i = 0; i < 8; ++i) {
      if (cin >> v[i]) {
      } else {
        return 0;
      }
    }
    cout << solve(init, v) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> ans = {0, 1, 2, 3, 4, 5, 6, 7};
vector<vector<int>> adj = {{1, 4}, {0, 2, 5}, {1, 3, 6}, {2, 7},
                           {0, 5}, {1, 4, 6}, {2, 5, 7}, {3, 6}};

int solve(vector<int> init) {
  map<vector<int>, int> d;
  d[init] = 0;
  queue<vector<int>> q;
  q.push(init);
  while (!q.empty()) {
    vector<int> v = q.front();
    q.pop();
    int step = d[v];
    if (v == ans)
      return step;
    for (int i = 0; i < 8; ++i) {
      if (v[i] == 0) {
        for (int j = 0; j < (int)adj[i].size(); ++j) {
          swap(v[i], v[adj[i][j]]);
          if (d.find(v) == d.end()) {
            q.push(v);
            d[v] = step + 1;
          }
          swap(v[i], v[adj[i][j]]);
        }
      }
    }
  }
}

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct pzl {
  vector<int> PV;
  int zero, cnt;
  pzl(vector<int> PV_, int zero_, int cnt_) {
    PV = PV_;
    zero = zero_;
    cnt = cnt_;
  }
};
pzl exswap(pzl a, int n) {
  swap(a.PV[a.zero], a.PV[a.zero + n]);
  a.zero += n;
  a.cnt++;
  return a;
}
int main() {
  map<vector<int>, int> M;
  vector<int> V(8);
  for (int i = 0; i < 8; i++)
    V[i] = i;
  queue<pzl> Q;
  pzl start(V, 0, 0);
  Q.push(start);
  while (!Q.empty()) {

    pzl a = Q.front();
    Q.pop();
    if ((a.PV == start.PV && a.cnt != 0) || M[a.PV])
      continue;
    M[a.PV] = a.cnt;
    if (a.zero > 3)
      Q.push(exswap(a, -4));
    if (a.zero < 4)
      Q.push(exswap(a, 4));
    if (a.zero != 0 && a.zero != 4)
      Q.push(exswap(a, -1));
    if (a.zero != 3 && a.zero != 7)
      Q.push(exswap(a, 1));
  }
  vector<int> inp(8);
  while (cin >> inp[0]) {
    for (int i = 1; i < 8; i++)
      cin >> inp[i];
    cout << M[inp] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
int dxy[] = {-4, -1, 1, 4};
struct exp {
  string str;
  int cnt, zero;
  exp(string str_, int cnt_, int zero_) {
    str = str_;
    cnt = cnt_;
    zero = zero_;
  }
};
int main() {
  char a[9];
  while (cin >> a[0]) {
    for (int i = 1; i < 8; i++)
      cin >> a[i];
    a[8] = 0;
    string pzl = string(a);
    int zerop;
    for (int i = 0; i < 8; i++)
      if (pzl[i] == '0')
        zerop = i;
    exp start(pzl, 0, zerop);
    queue<exp> Q;
    map<string, bool> used;
    Q.push(start);
    while (!Q.empty()) {
      exp c = Q.front();
      Q.pop();
      if (used[c.str])
        continue;
      used[c.str] = true;
      if (c.str == "01234567") {
        cout << c.cnt << endl;
        break;
      }
      for (int i = 0; i < 4; i++) {
        if (c.zero + dxy[i] < 0 || c.zero + dxy[i] >= 8)
          continue;
        exp next(c.str, c.cnt + 1, c.zero);
        swap(next.str[c.zero], next.str[c.zero + dxy[i]]);
        if (i == 1 && next.zero == 3)
          continue;
        if (i == 2 && next.zero == 4)
          continue;
        next.zero = c.zero + dxy[i];
        Q.push(next);
      }
    }
  }

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

#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

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

int swap(int s, int a, int b) {
  int buf[8];
  rep(i, 8) {
    buf[i] = s % 8;
    s /= 8;
  }
  swap(buf[a], buf[b]);
  int r = 0;
  rep(i, 8) {
    r *= 8;
    r += buf[7 - i];
  }
  return r;
}

int main() {
  int answer = 0;
  rep(i, 8) {
    answer *= 8;
    answer += (7 - i);
  }
  queue<pair<int, int>> q;
  q.push(make_pair(0, answer));
  map<int, int> dp;
  dp[answer] = 0;
  while (!q.empty()) {
    int c = q.front().first;
    int v = q.front().second;
    q.pop();
    int lz = -1;
    int t = v;
    rep(i, 8) {
      if (t % 8 == 0) {
        lz = i;
        break;
      }
      t /= 8;
    }
    rep(d, 4) {
      if (0 <= lz + dd[d] && lz + dd[d] < 8 &&
          !(lz == 3 && dd[d] == 1 || lz == 4 && dd[d] == -1)) {
        int nv = swap(v, lz + dd[d], lz);
        if (dp.find(nv) != dp.end())
          continue;
        dp[nv] = c + 1;
        q.push(make_pair(c + 1, nv));
      }
    }
  }
  int buf[8];
  while (scanf("%d", buf) != EOF) {
    rep(i, 7) scanf("%d", buf + i + 1);
    int s = 0;
    rep(i, 8) {
      s *= 8;
      s += buf[7 - i];
    }
    printf("%d\\n", dp[s]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

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

int swap(int s, int a, int b) {
  int buf[8];
  rep(i, 8) {
    buf[i] = s % 8;
    s /= 8;
  }
  swap(buf[a], buf[b]);
  int r = 0;
  rep(i, 8) {
    r *= 8;
    r += buf[7 - i];
  }
  return r;
}

bool is_answer(const vector<int> &v) {
  rep(i, 8) if (v[i] != i) return false;
  return true;
}

int main() {
  int answer = 0;
  rep(i, 8) {
    answer *= 8;
    answer += (7 - i);
  }
  int buf[8];
  while (scanf("%d", buf) != EOF) {
    rep(i, 7) scanf("%d", buf + i + 1);
    vector<int> v;
    rep(i, 8) v.push_back(buf[i]);
    int s = 0;
    rep(i, 8) {
      s *= 8;
      s += buf[7 - i];
    }
    queue<pair<int, int>> q;
    q.push(make_pair(0, s));
    set<int> visited;
    while (!q.empty()) {
      int c = q.front().first;
      int v = q.front().second;
      q.pop();
      if (visited.find(v) != visited.end())
        continue;
      visited.insert(v);
      if (v == answer) {
        printf("%d\\n", c);
        break;
      }
      int lz = -1;
      int t = v;
      rep(i, 8) {
        if (t % 8 == 0) {
          lz = i;
          break;
        }
        t /= 8;
      }
      rep(d, 4) {
        if (0 <= lz + dd[d] && lz + dd[d] < 8 &&
            !(lz == 4 && dd[d] == 1 || lz == 5 && dd[d] == -1)) {
          int nv = swap(v, lz + dd[d], lz);
          if (visited.find(nv) != visited.end())
            continue;
          q.push(make_pair(c + 1, nv));
        }
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

string swp(string s, const int &a, const int &b) {
  swap(s[a], s[b]);
  return s;
}

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

  string s = "01234567";

  queue<string> q;
  map<string, int> m;

  q.push(s);
  m[s] = 0;

  while (q.size()) {
    string t = q.front();
    q.pop();
    int idx = t.find('0');
    string u;
    if (idx - 1 >= 0 && idx != 4) {
      u = swp(t, idx, idx - 1);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
    if (idx + 1 < 8 && idx != 3) {
      u = swp(t, idx, idx + 1);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
    if (idx - 4 >= 0) {
      u = swp(t, idx, idx - 4);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
    if (idx + 4 < 8) {
      u = swp(t, idx, idx + 4);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
  }

  string line;
  stringstream ss;
  while (getline(cin, line)) {
    ss << line;
    string t = "";
    for (int i = 0; i < 8; ++i) {
      char c;
      ss >> c;
      t += c;
    }
    cout << m[t] << endl;
  }
}

Error: TLE

Fixed submission:

// 21:46~
//??????????????????????????£???
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>

using namespace std;

string data;
char goal[9] = "01234567";
queue<string> qu;
map<string, int> mp;
int d[4] = {-4, -1, 1, 4};
int pos;
int ans[1000];
char tmp;

int solve() {
  mp[data] = 0;
  qu.push(data);
  string pre_data;
  while (qu.size()) {
    data = qu.front();
    pre_data = data;
    qu.pop();
    if (data == goal)
      return mp[data];
    pos = data.find('0');
    for (int i = 0; i < 4; i++) {
      if (pos + d[i] >= 0 && pos + d[i] < 8) {
        swap(data[pos], data[pos + d[i]]);
        if (mp.find(data) == mp.end()) {
          mp[data] = mp[pre_data] + 1;
          qu.push(data);
        }
      }
    }
  }
  return mp[goal];
}

int main() {
  int n = 0;
  size_t c;
  while (getline(cin, data)) {
    while ((c = data.find_first_of(" ")) != string::npos) {
      data.erase(c, 1);
    }
    ans[n++] = solve();
    mp.clear();
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
}