h, w = map(int, input().split()) A = [] A.append([-1 for _ in range(w)]) for _ in range(h - 2): A.append(list(map(int, input().split()))) A.append([-1 for _ in range(w)]) INF = 10**18 DP = [[INF for _ in range(w)] for _ in range(h)] for i in range(h): if A[i][0] != -1: DP[i][0] = A[i][0] for j in range(w - 1): for i in range(h): if DP[i][j] == INF: continue for di in (-1, 0, 1): ni = i + di if A[ni][j + 1] == -1: continue DP[ni][j + 1] = min(DP[ni][j + 1], DP[i][j] + A[ni][j + 1]) ans = INF for i in range(h): ans = min(ans, DP[i][w - 1]) if ans == INF: ans = -1 print(ans)