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

bool acceptable(int** p) {
  for (int j = 0; j < 2; j++)
    for (int i = 0; i < 4; i++)
      if (p[i][j] != j * 4 + i) return false;

  return true;
}

int** move_0_up(int** p) {
  int val[4][2];

  for (int j = 0; j < 2; j++)
    for (int i = 0; i < 4; i++)
      val[i][j] = p[i][j];

  int x, y;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < 4; i++) {
      if (p[i][j] == 0) {
        x = i; y = j;
      }
    }
  }

  val[x][y] = p[x][(y == 0)? 1:0];
  val[x][(y == 0)? 1:0] = 0;

  return val;
}

int** move_0_right(int** p) {
  int val[4][2];

  for (int j = 0; j < 2; j++)
    for (int i = 0; i < 4; i++)
      val[i][j] = p[i][j];

  int x, y;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < 4; i++) {
      if (p[i][j] == 0) {
        x = i; y = j;
      }
    }
  }

  if (x == 3) return val;

  val[x][y] = p[x+1][y]