#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][15],vis[5000005];
struct node
{
int zt,step;
};
queue <node> q;
int bfs()
{
node q1;
q1.step=0;
q1.zt=(1<<n)-1;
q.push(q1);
vis[q1.zt]=1;
while (!q.empty())
{
node fir=q.front();
q.pop();
if (fir.zt==0) return fir.step;
for (int i=1;i<=m;i++)
{
int nx=fir.zt;
for (int j=1;j<=n;j++)
{
if (a[i][j]==1&&(nx&(1<<(j-1)))) nx^=(1<<(j-1));
if (a[i][j]==-1&&!(nx&(i<<(j-1)))) nx|=(1<<(j-1));
}
if (vis[nx]==0)
{
node now;
now.step=fir.step+1;
now.zt=nx;
q.push(now);
vis[nx]=1;
}
}
}
return -1;
}
int main ()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
cout<<bfs();
return 0;
}