#include"bits/stdc++.h" #include"atcoder/all" using namespace std; using namespace atcoder; using lint = long long; using ulint = unsigned long long; #define endl '\n' int const INF = 1<<30; lint const INF64 = 1LL<<61; lint const mod107 = 1e9+7; using mint107 = modint1000000007; long const mod = 998244353; using mint = modint998244353; lint ceilDiv(lint x, lint y){if(x >= 0){return (x+y-1)/y;}else{return x/y;}} lint floorDiv(lint x, lint y){if(x >= 0){return x/y;}else{return (x-y+1)/y;}} lint Sqrt(lint x){lint upper = 1e9;lint lower = 0;while(upper - lower > 0){lint mid = (1+upper + lower)/2;if(mid * mid > x){upper = mid-1;}else{lower = mid;}}return upper;} lint gcd(lint a,lint b){if(a&v){lint ans = INF64;for(lint i:v){ans = min(ans, i);}return ans;} lint chmax(vector&v){lint ans = -INF64;for(lint i:v){ans = max(ans, i);}return ans;} double dist(double x1, double y1, double x2, double y2){return sqrt(pow(x1-x2, 2) + pow(y1-y2,2));} string toString(lint n){string ans = "";if(n == 0){ans += "0";}else{while(n > 0){int a = n%10;char b = '0' + a;string c = "";c += b;n /= 10;ans = c + ans;}}return ans;} string toString(lint n, lint k){string ans = toString(n);string tmp = "";while(ans.length() + tmp.length() < k){tmp += "0";}return tmp + ans;} vectorprime;void makePrime(lint n){prime.push_back(2);for(lint i=3;i<=n;i+=2){bool chk = true;for(lint j=0;j>; graph g(200000); vecL(200000,-1); vecwent(200000,false); lint f(int l,lint x){ if(went[l])return 0; went[l]=true; L[l]=max(L[l],x); for(int v:g[l])f(v,L[l]); return 0; } int main(){ lint h,w; cin>>h>>w; vec>a(h,vec(w,-1)); repp(i,1,h-1){ rep(j,w)cin>>a[i][j]; } priority_queue>que; vec>d(h,vec(w+1,INF64)); repp(i,1,h-1)que.push({0,i,0}); while(!que.empty()){ auto q=que.top(); que.pop(); lint dd=-q[0]; lint x=q[1],y=q[2]; if(y==w)continue; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ int nx=x+i,ny=y+j; if(x+i<=0)continue; if(y+j<=0)continue; if(nx>=h-1)continue; if(ny>w)continue; if(d[nx][ny]>dd+a[nx][ny-1]){ d[nx][ny]=dd+a[nx][ny-1]; que.push({-d[nx][ny],nx,ny}); } } } } lint ans=INF64; rep(i,h)ans=min(ans,d[i][w]); if(ans==INF64)ans=-1; cout<