H,W = map(int,input().split()) A = [] A.append([-1]*W) for _ in range(H-2): A.append(list(map(int,input().split()))) A.append([-1]*W) INF = float('inf') dp = [[INF]*W for _ in range(H)] for i in range(H): if A[i][0] == -1: continue dp[i][0] = A[i][0] for j in range(1,W): for i in range(1,H): if A[i][j] == -1: continue tmp = min(dp[i-1][j-1], dp[i][j-1], dp[i+1][j-1]) dp[i][j] = tmp + A[i][j] ans = INF for i in range(H): ans = min(ans, dp[i][-1]) print(-1 if ans==INF else ans)