#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 60

void get_int2(char *line, int size, int *a1, int *a2) {
  if(!fgets(line, size, stdin)) return;
  sscanf(line, "%d %d", a1, a2);
}

struct state {
  int a;
  int b;
  int c;
  int cnt;
};

#define QUEUE_MAX 15000000

static struct state queue[QUEUE_MAX];
static int start = 0;
static int end = 0;

void enqueue(struct state s) {
  queue[start++] = s;
}

struct state dequeue(void) {
  return queue[end++];
}

int is_empty(void) {
  return start == end;
}

#define HTABLE_SIZE 2097152
#define HTABLE_MASK HTABLE_SIZE-1
#define ELEM_POOL_SIZE QUEUE_MAX

struct list {
  struct list *next;
  unsigned int num;
};
static struct list htable[HTABLE_SIZE];
static struct list pool[ELEM_POOL_SIZE];
static int pos = 0;

void initialize(void) {
  start = end = 0;
  int i;
  pos = 0;
  for(i = 0; i < HTABLE_SIZE; i++) {
    struct list *head = &htable[i];
    head->next = head;
  }
}

int get_key(unsigned int num) {
  return num&HTABLE_MASK;
}


void create_htable(void) {
  int i;
  for(i = 0; i < HTABLE_SIZE; i++) {
    htable[i].next = &htable[i];
  }
  return;
}

unsigned int get_unique_id(struct state s) {
  return (unsigned int)(s.a << 16) + s.b;
}

int find_state(struct state s) {
  unsigned int num = get_unique_id(s);
  struct list *head = &htable[get_key(num)];
  struct list *tmp;
  for(tmp = head->next; tmp != head; tmp = tmp->next) {
    if(tmp->num == num) return 1;
  }
  return 0;
}


void keep_track(struct state s) {
  unsigned int num = get_unique_id(s);
  struct list *head = &htable[get_key(num)];
  struct list *elem = &pool[pos++];
  elem->next = head->next;
  elem->num = num;
  head->next = elem;
  return;
}

int get_max(int num) {
  if(!num) return 0;
  return 31 - __builtin_clz(num);
  // int i;
  // for(i = 31; i >= 0; i--) {
  //   if(1<<i & num) return i;
  // }
  // return 0;
}

void get_area_state(char *line, int *a) {
  int i;
  char *tmp = strtok(line, " ");
  int k = strtol(tmp, NULL, 10);
  for(i = 0; i < k; i++) {
    tmp = strtok(NULL, " ");
    *a |= (1 << strtol(tmp, NULL, 10));
  }
  return;
}

void move(int *from, int *to, int bits) {
  *from &= ~bits;
  *to |= bits;
}

int main(void) {
  char line[BUF_SIZE];

  int i, j;
  while(1) {
    int num, limits;
    get_int2(line, BUF_SIZE, &num, &limits);
    if(!num && !limits) break;
    struct state st = {0, 0, 0, 0};
    if(!fgets(line, BUF_SIZE, stdin)) return 0;
    get_area_state(line, &st.a);
    if(!fgets(line, BUF_SIZE, stdin)) return 0;
    get_area_state(line, &st.b);
    if(!fgets(line, BUF_SIZE, stdin)) return 0;
    get_area_state(line, &st.c);

    initialize();
    enqueue(st);
    keep_track(st);
    int flag = -1;
    while(!is_empty()) {
      struct state s = dequeue();
      if(!s.b && (!s.a || !s.c)) {
        flag = s.cnt;
        break;
      }
      int a_max = get_max(s.a);
      int b_max = get_max(s.b);
      int c_max = get_max(s.c);
      int max[3] = {a_max, b_max, c_max};
      if(s.cnt >= limits) continue;
      for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
          // including i==j
          if(i+j==2) continue; // (0,2) or (2,0) is not allowed
          if(max[i] <= max[j]) continue;
          // assume that idx:i is bigger than idx:j
          struct state ns = s; ns.cnt = s.cnt + 1;
          int bits = 1 << max[i];
          int *areas[] = {&ns.a, &ns.b, &ns.c};
          // from, to, bits
          move(areas[i], areas[j], bits);
          if(!find_state(ns)) {
            keep_track(ns);
            enqueue(ns);
          }
        }
      }
    }
    printf("%d\n", flag);
  }
  return 0;
}


Measured memory_bytes: 112704