#include <bits/stdc++.h>
using namespace std;
struct zt_bs{
int zt;
int bs;
};
int n,m;
int k[101][11];
bool f[2048];
queue<zt_bs>q;
int now;
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>k[i][j];
f[(1<<n)-1]=1;
q.push( (zt_bs) {(1<<n)-1,0} );
while(!q.empty()){
int fz=q.front().zt;
int fb=q.front().bs;
q.pop();
if(fz==0){
cout<<fb<<endl;
return 0;
}
for(int i=1;i<=m;i++){
now=fz;
for(int j=1;j<=n;j++){
if(k[i][j]==1&&(fz&(1<<j-1)))
now^=(1<<j-1);
else if(k[i][j]==-1&&(!fz&(1<<j-1)))
now|=(1<<j-1);
}
if(!f[now]){
q.push( (zt_bs) {now,fb+1} );
f[now]=1;
}
}
}
cout<<-true;
return 0;
}