#include using namespace std; using ll = long long; constexpr ll INF64 = 1LL<<60; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main() { int h, w; cin >> h >> w; h -= 2; vector a(h, vector(w, 0ll)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; if (a[i][j] == -1) { a[i][j] = INF64; } } } vector dp(h, vector(w, INF64)); for (int i = 0; i < h; i++) { dp[i][0] = a[i][0]; } for (int j = 0; j < w - 1; j++) { for (int i = 0; i < h; i++) { for (const auto diff : { -1, 0, 1 }) { auto nxt = i + diff; if (0 <= nxt && nxt < h) { chmin(dp[nxt][j + 1], a[nxt][j + 1] + dp[i][j]); } } } } ll ans = INF64; for (int i = 0; i < h; i++) { chmin(ans, dp[i][w - 1]); } cout << (ans == INF64 ? -1 : ans) << endl; return 0; }