#include<iostream>
#include<math.h>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<stdio.h>
using namespace std;
struct node
{
int ne;
int x;
int y;
int s;
};
int n,m;
int a[105][105];
bool f[105][105][10];
queue<node> q;
int bfs()
{
node now,next;
now.x=1;
now.y=1;
now.s=0;
now.ne=-1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==n && now.y==m)
{
return now.s;
}
int dx[8]={a[now.x][now.y],0-a[now.x][now.y],0,a[now.x][now.y],a[now.x][now.y],-a[now.x][now.y],-a[now.x][now.y]};
int dy[8]={0,0,a[now.x][now.y],0-a[now.x][now.y],a[now.x][now.y],-a[now.x][now.y],a[now.x][now.y],-a[now.x][now.y]};
for(int i=0;i<8;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if(next.x>=1 && next.y<=n && next.y>=1 && next.y<=m && !f[next.x][next.y][i] && i!=now.ne)
{
next.ne=i;
f[next.x][next.y][i]=1;
next.s=now.s+1;
q.push(next);
}
}
}
return -1;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
int ans=bfs();
if(ans==-1)
{
cout<<"NEVER";
}
else
{
cout<<ans;
}
return 0;
}