#include using namespace std; using ll = long long; int main(){ // input int H,W; cin >> H >> W; H -= 2; vector> A(W,vector(H)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> A[j][i]; // solve: dp vector> dp(W+1,vector(H,9e18)); for (int j = 0; j < H; j++) dp[0][j] = 0; for (int i = 0; i < W; i++){ for (int j = 0; j < H; j++){ ll cost = A[i][j]; if(cost == -1) continue; dp[i+1][j] = min(dp[i+1][j],dp[i][j]+cost); if(j - 1 >= 0) dp[i+1][j] = min(dp[i+1][j],dp[i][j-1]+cost); if(j + 1 < H) dp[i+1][j] = min(dp[i+1][j],dp[i][j+1]+cost); // cout << i << " " << j << " " << dp[i+1][j] << endl; } } // output ll ans = 9e18; for (int j = 0; j < H; j++) ans = min(ans,dp[W][j]); cout << (ans < 9e18? ans : -1) << endl; }