#include <bits/stdc++.h>
using namespace std;
int m,n;
int city[105][105],vis[105][105];
int pos[2][8]= {{0,0,1,-1,-1,-1,1,1},{1,-1,0,0,-1,1,-1,1}};
int step=99999999;
struct node
{
int x,y,ban,ans;
};
queue<node> Q;
int main()
{
memset(vis,0,sizeof(vis));
cin>>n>>m;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cin>>city[i][j];
}
}
node start;
start.x=1,start.y=1,start.ans=0,start.ban=-1;
Q.push(start);
while(!Q.empty())
{
node now = Q.front();
Q.pop();
if(now.x == m && now.y == n)
{
cout<<now.ans;
return 0;
}
for(int i=0;i<8;i++)
{
int x2,y2;
if(i != now.ban)
{
x2 = now.x + city[now.x][now.y] * pos[0][i];
y2 = now.y + city[now.x][now.y] * pos[1][i];
}
else{continue;}
if(x2 >= 1 && x2 <= m && y2 >=1 && y2 <= n && !vis[x2][y2])
{
node next;
next.ans=now.ans+1;
next.x=x2;
next.y=y2;
next.ban=i;
Q.push(next);
vis[x2][y2] = 1;
}
}
}
cout<<"NEVER";
return 0;
}