#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int adj[12][22][22], dp[12][(1<<20)+5];
int n[12], tong[22];
int l, k, ans = 1e6;

int dem(int mask){
    int cnt = 0;
    while (mask){
        cnt++;
        mask = mask&(mask-1);
    }
    return cnt;
}

int check(int cl, int mask){
    if (cl > l){
        if (dem(mask) >= k) return 1;
        return 0;
    }

    for (int i = 0; i < n[cl]; i++) tong[i] = 0;
    for (int u = 0; u < n[cl-1]; u++){
        if ((mask>>u)&1){
            for (int v = 0; v < n[cl]; v++){
                tong[v] += adj[cl][u][v];
            }
        }
    }

    int nmask = 0;
    for (int i = 0; i < n[cl]; i++){
        if (tong[i] > 0) nmask = nmask|(1<<i);
    }

    if (dp[cl][nmask] != -1) return dp[cl][nmask];
    return dp[cl][nmask] = check(cl+1, nmask);
}

void solve(){
    for (int i = 1; i <= l; i++){
        for (int j = 0; j <= (1<<n[i]); j++) dp[i][j] = -1;
    }

    for (int mask = 1; mask < (1<<n[1]); mask++){
        if (check(2, mask) == 1){
            ans = min(ans, dem(mask));
        }
    }

    if (ans == 1e6) cout << "-1\n";
    else cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> l >> k;
    for (int i = 1; i <= l; i++) cin >> n[i];
    for (int i = 1; i < l; i++){
        for (int j = 0; j < n[i]; j++){
            for (int d = 0; d < n[i+1]; d++){
                cin >> adj[i+1][j][d];
            }
        }
    }

    solve();
}