#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
    return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}


class state {
private:
    static int st_pow(int i) {
        static unordered_map<int, int> pow; // memo
        if (pow[i]) return pow[i];

        if (i == 0) {
            return pow[i] = 1;
        } else {
            return pow[i] = n * st_pow(i - 1);
        }
    }
public:
    static constexpr int n = 3;

    array<deque<int>, n> trays;

    vector<int> get_neighbor() {
        vector<int> ret;

        REP(i, n) {
            if (trays[i].empty()) continue;

            if (i != 0) { // not left side
                if (trays[i-1].empty() || trays[i-1].back() < trays[i].back()) {
                    state n_st = *this;
                    n_st.trays[i-1].push_back(n_st.trays[i].back());
                    n_st.trays[i].pop_back();
                    ret.push_back(encode(n_st));
                }
            }

            if (i != n-1) { // not right side
                if (trays[i+1].empty() || trays[i+1].back() < trays[i].back()) {
                    state n_st = *this;
                    n_st.trays[i+1].push_back(n_st.trays[i].back());
                    n_st.trays[i].pop_back();
                    ret.push_back(encode(n_st));
                }
            }
        }

        return ret;
    }

    void print() const { // for debug
        cout << "================" << endl;
        REP(i, n) {
            cout << "cup: " << i + 1 << endl;
            for (int cup : trays[i]) {
                cout << cup << " ";
            }
            cout << endl;
        }
    }

    static int encode(const state& st) { // state => int
        int ret = 0;

        REP(i, n) {
            for (int cup : st.trays[i]) {
                ret += st_pow(cup) * i;
            }
        }
        
        return ret;
    }

    static state decode(int cups, int num) { // int => state
        state st;

        for (int i = 0; i < cups; i++) {
            st.trays[num % n].push_back(i);
            num /= n;
        }

        return st;
    }

    static bool is_moderate(const int cups, const state& st) {
        return  st.trays[0].size() == cups || st.trays[n-1].size() == cups; 
    }

    static int solve(const int cups, const int max, const state& st) {
        queue<int> q;
        vector<int> score(st_pow(cups), -1);

        int e_st = encode(st);
        q.push(e_st);
        score[e_st] = 0;

        while (!q.empty()) { // bfs
            int e_now = q.front(); q.pop();
            state now = decode(cups, e_now);

            if (score[e_now] == max) continue;

            if (is_moderate(cups, now)) { // return ans
                return score[e_now];
            }

            for (const int e_neighbor : now.get_neighbor()) {
                if (score[e_neighbor] == -1) {
                    q.push(e_neighbor);
                    score[e_neighbor] = score[e_now] + 1;
                }
            }
        }

        return -1; // not found
    }
};

int main() {
    int n, m;
    while (cin >> n >> m, n && m) {
        state st;
        REP(i, state::n) {
            int c; cin >> c;
            REP(j, c) {
                int num; cin >> num;
                --num;
                st.trays[i].push_back(num);
            }
        }
        cout << state::solve(n, m, st) << endl;
    }

    return 0;
}



Measured memory_bytes: 58936