P26511 [AtCoder] JOI 2006 Preliminary Problem 4
提高
题目描述

Problem Description

There are $ n $ cups of different sizes and 3 trays (trays) A, B, C. These cups are stacked in piles on the trays, with the smallest cup at the bottom and the second smallest cup on top of it, and so on, in each tray. For example, the right side of the figure below shows a state where $ n = 5 $ cups are stacked on trays A, B, C with 2 cups, 0 cups, and 3 cups respectively.

2006-yo-t4-fig1.png

Given the initial state of the cups, we want to determine how many moves are needed to move all the cups to either tray A or C while following the rules 1 to 3:

(Rule 1) Only one cup can be moved at a time. It must be the top cup of its tray (the largest cup).
(Rule 2) A smaller cup cannot be placed on top of a larger cup.
(Rule 3) Direct moves of a single cup are only allowed from tray A to B, B to A, B to C, and C to B. Direct moves from A to C or C to A are not allowed.

Given the initial state of $ n $ cups and an integer $ m $, determine if it is possible to stack all the cups on either tray A or C within $ m $ moves. If possible, output the minimum number of moves required. If not possible, output $ -1 $.

The first line of input contains $ n $ and $ m $ separated by a space. $1 \leq n \leq 15$ and $1 \leq m \leq 15,000,000$. The second, third, and fourth lines contain integers from 1 to $ n $ divided into three groups, sorted in ascending order within each group. The number of integers in each group is written at the beginning of each line. The integers (excluding the first one) on the second line represent the sizes of the cups stacked on tray A. Similarly, the integers on the third line represent the sizes of the cups on tray B, and the integers on the fourth line represent the sizes of the cups on tray C.

In the output, print the number of moves or $ -1 $ followed by a newline.


Input Format

None

Output Format

None

Sample Input and Output

Sample Input #1

3 10
0
1 1
2 2 3

Sample Output #1

9

Sample Input #2

4 20
2 1 2
1 3
1 4

Sample Output #2

3

Sample Input #3

2 5
2 1 2
0
0

Sample Output #3

0

Sample Input #4

3 3
0
1 1
2 2 3

Sample Output #4

-1

Notes/Hints

Sample Explanation 1

- - - - - -

Sample Explanation 2

- - - - - -

Sample Explanation 3

- - - - - -

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 10 0 1 1 2 2 3
样例输出
9