1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4
5 int g[6][6] =
6 {
7 {0,0,0,0,0,0},
8 {0,1,1,1,1,1},
9 {0,0,1,1,1,1},
10 {0,0,0,2,1,1},
11 {0,0,0,0,0,1},
12 {0,0,0,0,0,0}
13 };
14
15 int a[6][6];
16
17 int xx[8] = { 1,-1,2,-2,1,-1,2,-2 };
18 int yy[8] = { 2,2,1,1,-2,-2,-1,-1 };
19
20 int pre()
21 {
22 int re = 0;
23 for (int i = 1; i <= 5; i++)
24 {
25 for (int j = 1; j <= 5; j++)
26 {
27 if (a[i][j] != g[i][j]) re++;
28 }
29 }
30 return re;
31 }
32
33 bool ida(int maxd, int d, int x, int y)
34 {
35 int t = pre();
36 if (!t) return true;
37 if (t + d - 1 > maxd) return false;
38 for (int i = 0; i < 8; i++)
39 {
40 if (x + xx[i] < 1 || x + xx[i]>5 || y + yy[i] < 1 || y + yy[i]>5) continue;
41 swap(a[x + xx[i]][y + yy[i]], a[x][y]);
42 if (ida(maxd, d + 1, x + xx[i], y + yy[i])) return true;
43 swap(a[x + xx[i]][y + yy[i]], a[x][y]);
44 }
45 return false;
46 }
47
48 int T;
49
50 int main()
51 {
52 cin >> T;
53 cin.ignore(0x3f, '\n');
54 while (T--)
55 {
56 int x, y;
57 for (int i = 1; i <= 5; i++)
58 {
59 char t[10];
60 scanf(" %s", t + 1);
61 //scanf_s(" %s", t + 1, 6);
62 for (int j = 1; j <= 5; j++)
63 {
64 a[i][j] = t[j] - '0';
65 if (a[i][j] && a[i][j] - 1)
66 {
67 a[i][j] = 2;
68 x = i;
69 y = j;
70 }
71 }
72 }
73 bool flag = false;
74 for (int i = 1; i <= 15; i++)
75 {
76 if (ida(i, 0, x, y))
77 {
78 cout << i << endl;
79 flag = true;
80 break;
81 }
82 }
83 if(!flag) cout << -1 << endl;
84 }
85 }