using namespace std; #include void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout< inline bool chmax(ll& a, ll b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(ll& a, ll b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, star,fini) for (int i = star; i < fini; i++) #define ALL(x) std::begin(x), std::end(x) #define INF ((1LL<<62)-(1LL<<31)) #define bit(x,i)(((x)>>(i))&1) long double distance(long double xi,long double yi,long double xj,long double yj){return sqrt(1.0*((xi-xj)*(xi-xj))+1.0*((yi-yj)*(yi-yj)));} ll m[1000][1000],dp[1000][1000]; void _main(){ ll h,w;cin >> h >> w; rep(i,0,h)rep(j,0,w)m[i][j]=-1,dp[i][j]=INF; rep(i,1,h-1)rep(j,0,w)cin >> m[i][j]; rep(j,0,h)dp[j][0]=0; rep(i,1,h-1)dp[i][0]=m[i][0]; rep(i,1,w){ rep(j,1,h-1){ if(m[j][i]!=-1){ if(m[j-1][i-1]!=-1)chmin(dp[j][i],dp[j-1][i-1]+m[j][i]); if(m[j][i-1]!=-1)chmin(dp[j][i],dp[j][i-1]+m[j][i]); if(m[j+1][i-1]!=-1)chmin(dp[j][i],dp[j+1][i-1]+m[j][i]); } } } ll ans = INF; rep(i,0,h){ chmin(ans,dp[i][w-1]); } cout<<(ans!=INF?ans:-1)<