#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
int Case = 1;
struct node{
    int s, num;
};
int a[105][1050], vis[1000005];
int n, m;
int bfs() {
    queue<node>Q;
    Q.push(node{((1<<n)-1), 0});
    vis[(1<<n)-1] = 1;
    while(!Q.empty()) {
        node t = Q.front();Q.pop();
        if(!t.s) return t.num;
        for(int i = 1; i <= m; i++) {
            int temp = t.s;
            for(int j = 1; j <= n; j++) {
                if(a[i][j] == 1 && (temp&(1<<(j-1)))) temp = temp^(1<<(j-1));
                else if(a[i][j] == -1 && !(temp&(1<<(j-1)))) temp = temp|(1<<(j-1));
            }
            if(!vis[temp])vis[temp] = 1,
            Q.push(node{temp, t.num+1});
        }
    }
    return -1;
}
void solve() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= n; j++) {
            scanf("%d", &a[i][j]);
        }
    }
    printf("%d\n", bfs());
    return;
}

int main() {
    while(Case--) {
        solve();
    }
    return 0;
}