Parks of Jakarta

Given an initial stacking of N bricks over three parks plus up to 16 target configurations, find the cheapest move sequence visiting all targets and ending with all bricks in one park.

Hard8GraphShortest pathBrute forceBit manipulationNo attempts yetTime limit2sMemory limit512 MB

Problem

There are three parks in Jakarta: Park 1, Park 2, and Park 3. The new governor of Jakarta wants to decorate these parks by stacking bricks in them. There are NN bricks in Jakarta, numbered from 11 to NN, where a smaller number means a smaller brick. A bigger brick must never sit on top of a smaller brick, so brick ii may be placed directly on top of brick jj only when i<ji < j.

A brick configuration is a distribution of the NN bricks over the three parks. Each park holds at most one stack of bricks, and every stack must respect the rule above. For example, when N=3N = 3, one valid configuration keeps brick 11 (on top) and brick 22 (at the bottom) in Park 1, leaves Park 2 empty, and keeps brick 33 in Park 3.

One operation transforms a configuration as follows.

  • Choose two different parks ii and jj. Take the topmost brick of the stack in Park ii and place it on top of the stack in Park jj. The operation is invalid when Park ii is empty, and it is also invalid when the resulting stack in Park jj breaks the stacking rule.
  • Moving a brick requires a rented truck, so each move from Park ii to Park jj costs Ri,jR_{i,j}. The cost from Park ii to Park jj may differ from the cost from Park jj to Park ii.

The process starts from a given initial configuration. The governor wants to see each of MM chosen configurations at least once, in any order. At the end, all bricks must stand stacked in a single park. Find the minimum possible total cost.

Formally, let the desired configurations be G1,G2,,GMG_1, G_2, \dots, G_M. Find a sequence of configurations C0,C1,,CkC_0, C_1, \dots, C_k (k0k \ge 0) with minimum cost such that:

  • C0C_0 is the initial configuration.
  • For every desired configuration GG there is an integer xx (0xk0 \le x \le k) with Cx=GC_x = G. The visiting order does not matter.
  • In CkC_k all bricks are stacked in one park.
  • For every 0i<k0 \le i < k, Ci+1C_{i+1} is obtained from CiC_i by a single operation.
  • The cost of the sequence is the sum of the single-operation costs over all 0i<k0 \le i < k.

Input

The first line contains two integers NN MM (1N401 \le N \le 40, 0M160 \le M \le 16): the number of bricks and the number of configurations to visit. Each of the next three lines contains three integers. The jj-th integer on the ii-th line is Ri,jR_{i,j} (0Ri,j10000 \le R_{i,j} \le 1000, Ri,i=0R_{i,i} = 0), the cost of moving one brick from Park ii to Park jj. The next three lines describe the initial configuration, followed by MM blocks of three lines describing the desired configurations. Every configuration uses the following format.

One configuration is written in three lines. The ii-th line starts with an integer KK (0KN0 \le K \le N), the number of bricks in Park ii, followed by KK integers A1,A2,,AKA_1, A_2, \dots, A_K (1AiN1 \le A_i \le N) naming the bricks in Park ii. For all 1i<jK1 \le i < j \le K, Ai<AjA_i < A_j holds, and every integer from 11 to NN appears exactly once across the three lines.

Output

Print the minimum total cost that satisfies the governor's demand, in one line.

Hint

Explanation of the first case

In the first case, Park 1 initially holds bricks 11 and 22 while Park 3 holds brick 33. Since M=0M = 0, it suffices to gather all bricks into one park, which can be done with cost 55 as follows.

  • Move brick 33 from Park 3 to Park 2. This costs 11.
  • Move brick 11 from Park 1 to Park 2. This costs 11.
  • Move brick 11 from Park 2 to Park 3. This costs 11.
  • Move brick 22 from Park 1 to Park 2. This costs 11.
  • Move brick 11 from Park 3 to Park 2. This costs 11.

All bricks are now in Park 2 with total cost 55, and no cheaper sequence exists. Note that the number of operations is not minimized.

Explanation of the second case

In the second case, the optimum visits the second desired configuration before the first one. From the initial configuration, the second configuration is reachable in 44 operations with total cost 88. From the second configuration, the first configuration is reachable in 77 operations with total cost 1414. Since the first configuration already stacks all bricks in one park, no further operation is needed. The total cost is therefore 2222.