#if 1 #include const long long MOD = 998244353; #include using namespace atcoder; typedef modint998244353 mint93; typedef modint1000000007 mint17; typedef modint mint; using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair; #define vec vector template using v = vector; template using vv = v>; template using vvv = v>; using vl = v; using vvl = vv; using vvvl = vvv; using vpl = v; using UF = dsu; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } #define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, N) for (ll i = 0; i < (ll)(N); i++) #define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++) #define rrep(i, N) for (ll i = N - 1; i >= 0; i--) #define rrep1(i, N) for (ll i = N; i > 0; i--) #define fore(i, a) for (auto &i : a) #define fs first #define sc second #define eb emplace_back #define pb push_back #define ps push #define elif else if #define all(x) (x).begin(), (x).end() #define UNIQUE(x) (x).erase(unique((x).begin(), (x).end()), (x).end()); #define YES(x) cout << ((x) ? "YES\n" : "NO\n"); #define Yes(x) cout << ((x) ? "Yes\n" : "No\n"); #define yes(x) cout << ((x) ? "yes\n" : "no\n"); #define sum(x) accumulate(all(x), 0ll) template void chmin(T &t, const U &u) { if (t > u) t = u; } template void chmax(T &t, const U &u) { if (t < u) t = u; } template T min(v &lis) { return *min_element(all(lis)); } template T max(v &lis) { return *max_element(all(lis)); } const int inf = (1 << 29); const ll infl = (1LL << 60); const ll mod93 = 998244353ll; const ll mod17 = 1000000007ll; int popcnt(uint x) { return __builtin_popcount(x); } int popcnt(ull x) { return __builtin_popcountll(x); } int bsr(uint x) { return 32 - __builtin_clz(x); } int bsr(ull x) { return 64 - __builtin_clzll(x); } int bsf(uint x) { return __builtin_ctz(x); } int bsf(ull x) { return __builtin_ctzll(x); } template istream &operator>>(istream &is, vector &x) { for (auto &y : x) is >> y; return is; } template ostream &operator<<(ostream &os, vector &x) { for (unsigned int i = 0, size = x.size(); i < size; i++) os << x[i] << (i == size - 1 ? "" : " "); return os; } template istream &operator>>(istream &is, pair &x) { return is >> x.first >> x.second; } template ostream &operator<<(ostream &os, pair &x) { return os << x.first << " " << x.second; } template T popleft(queue &q){ T a = q.front(); q.pop(); return a; } struct StopWatch { bool f = false; clock_t st; void start() { f = true; st = clock(); } int msecs() { assert(f); return (clock() - st) * 1000 / CLOCKS_PER_SEC; } }; ll rand_int(ll l, ll r) { // [l, r] static random_device rd; static mt19937 gen(rd()); return uniform_int_distribution(l, r)(gen); } template struct binomial { long long n; std::vector> fact, ifact; binomial(long long n) : n(n), fact(n + 1), ifact(n + 1) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i > 0; i--) ifact[i - 1] = ifact[i] * i; } atcoder::static_modint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; #endif // #define _GLIBCXX_DEBUG int main() { ll h, w; cin >> h >> w; vvl a(h - 2, vl(w)); cin >> a; ll ans = infl; rep(i, h - 2){ if (a[i][0] == -1) continue; queue q; q.push({i, 0, a[i][0]}); while (!q.empty()){ vl b = popleft(q); if (b[1] == w - 1){ chmin(ans, b[2]); continue; } for (ll j = -1; j <= 1; j++) { if (h - 3 >= b[0] + j && b[0] + j >= 0 && a[b[0] + j][b[1] + 1] != -1){ q.push({b[0] + j, b[1] + 1, b[2] + a[b[0] + j][b[1] + 1]}); } } } } if (ans == infl) cout << -1 << endl; else cout << ans << endl; }