#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int n,m;
int k3[22];

int main(void){
  k3[0] = 1;
  for(int i = 1; i <= 15; i++) k3[i] = k3[i-1]*3;

  while(cin >> n >> m && n){
    queue<int> que[3];
    for(int i = 0; i < 3; i++){
      int k;
      cin >> k;
      for(int j = 0; j < k; j++){
	int d;
	cin >> d;
	que[i].push(d);
      }
    }


    int s = 0;
    int c = 0;
    for(int i = n-1; i >= 0; i--){
      int j;
      for(j = 0; que[j].empty() || n-i != que[j].front(); j++);
      que[j].pop();
      int d = abs(j-s);
      if(d == 1){
	c += k3[i];
	s = abs(s-2);
      }else if(d == 2){
	c += k3[i]*2;
      }
    }

    int res = min(c,k3[n]-1-c);
    if(res > m) cout << -1 << endl;
    else cout << res << endl;
  }
}