#include <bits/stdc++.h>

using namespace std;

int pow3[16];
int n, m;

void calc_pow3()
{
    pow3[0] = 1;

    for (int i = 1; i <= 15; ++i) {
        pow3[i] = pow3[i - 1] * 3;
    }
}

int solve(int n, int m)
{
    int c[16];
    for (int i = 0; i < 3; ++i) {
        int j;

        cin >> j;

        int temp;
        for (int k = 0; k < j; ++k) {
            cin >> temp;
            
            c[n - temp] = i;
        }
    }

    int ans = 0;
    int d;
    int p = 0;

    for (int i = n - 1; i >= 0; --i) {
        d = abs(c[i] - p);
        
        ans += pow3[i] * d;
        
        if (d == 1) p = 2 - p;


    }

    int ans2;

    ans2 = pow3[n] - 1 - ans;

    ans = min(ans, ans2);

    ans = (ans > m) ? -1: ans;

    return ans;
}

int main()
{
    calc_pow3();
    
    int n, m;

    while (cin >> n >> m) {
        if (n == 0 && m == 0) break;

        cout << solve(n, m) << endl;
    }

    return 0;
}