INF = float('inf') N, M = map(int, input().split()) if N == 1: exit(print(0)) A = [list(map(int, input().split())) for _ in range(N)] dp = [0] * M for i in range(N): ndp = [dp[j] + A[i][j] for j in range(M)] m = min(ndp) nndp = [min(ndp[j], m + A[i][j]) for j in range(M)] dp = nndp print(min(dp))