{"trustable":true,"prependHtml":"\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003eLet\u0027s play a card game called Gap. \u003cbr\u003eYou have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card.\u003cbr\u003e\u003cbr\u003eFirst, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout.\u003cbr\u003e\u003cbr\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/d012b12a0f6356b6e19e5d8d1f5235e9?v\u003d1714334343\"\u003e\u003cbr\u003e\u003cbr\u003eNext, you remove all cards of value 1, and put them in the open space at the left end of the rows: \"11\" to the top row, \"21\" to the next, and so on.\u003cbr\u003e\u003cbr\u003eNow you have 28 cards and four spaces, called gaps, in four rows and eight columns. You start moving cards from this layout.\u003cbr\u003e\u003cbr\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/c1bf4588c501e78123578e253796f1eb?v\u003d1714334343\"\u003e\u003cbr\u003e\u003cbr\u003eAt each move, you choose one of the four gaps and fill it with the successor of the left neighbor of the gap. The successor of a card is the next card in the same suit, when it exists. For instance the successor of \"42\" is \"43\", and \"27\" has no successor.\u003cbr\u003e\u003cbr\u003eIn the above layout, you can move \"43\" to the gap at the right of \"42\", or \"36\" to the gap at the right of \"35\". If you move \"43\", a new gap is generated to the right of \"16\". You cannot move any card to the right of a card of value 7, nor to the right of a gap.\u003cbr\u003e\u003cbr\u003eThe goal of the game is, by choosing clever moves, to make four ascending sequences of the same suit, as follows.\u003cbr\u003e\u003cbr\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/975bb9add470f678fed43aff1ea7352d?v\u003d1714334343\"\u003e\u003cbr\u003e\u003cbr\u003eYour task is to find the minimum number of moves to reach the goal layout.\u003cbr\u003e\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"The input starts with a line containing the number of initial layouts that follow.\u003cbr\u003e\u003cbr\u003eEach layout consists of five lines - a blank line and four lines which represent initial layouts of four rows. Each row has seven two-digit numbers which correspond to the cards.\u003cbr\u003e"}},{"title":"Output","value":{"format":"HTML","content":"For each initial layout, produce a line with the minimum number of moves to reach the goal layout. Note that this number should not include the initial four moves of the cards of value 1. If there is no move sequence from the initial layout to the goal layout, produce \"-1\".\u003cbr\u003e"}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\r\n\r\n12 13 14 15 16 17 21\r\n22 23 24 25 26 27 31\r\n32 33 34 35 36 37 41\r\n42 43 44 45 46 47 11\r\n\r\n26 31 13 44 21 24 42\r\n17 45 23 25 41 36 11\r\n46 34 14 12 37 32 47\r\n16 43 27 35 22 33 15\r\n\r\n17 12 16 13 15 14 11\r\n27 22 26 23 25 24 21\r\n37 32 36 33 35 34 31\r\n47 42 46 43 45 44 41\r\n\r\n27 14 22 35 32 46 33\r\n13 17 36 24 44 21 15\r\n43 16 45 47 23 11 26\r\n25 37 41 34 42 12 31\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e0\r\n33\r\n60\r\n-1\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}