N,M = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(N)] INFTY = 10**13 dp = [[INFTY for _ in range(M)] for _ in range(N)] dpmin = [INFTY]*N for j in range(M): dp[0][j] = 0 dpmin[0] = min(dpmin[0],dp[0][j]) if N>=2: for j in range(M): dp[1][j] = A[0][j]+A[1][j] dpmin[1] = min(dpmin[1],dp[1][j]) for i in range(2,N): for j in range(M): dp[i][j] = dp[i-1][j]+A[i][j] if dpmin[i-1]!=dp[i-1][j]: dp[i][j] = min(dp[i][j],dpmin[i-1]+A[i-1][j]+A[i][j]) dpmin[i] = min(dpmin[i],dp[i][j]) print(min(dp[N-1]))