n,m=list(map(int,input().split())) a=[] for _ in range(n): a.append(list(map(int,input().split()))) dp=[[0]*(m) for _ in range(n)] mi=[0]*(m) for i in range(n-1): for j in range(m): if dp[i][j]==mi[i] and i!=0: dp[i+1][j]=dp[i][j]+a[i+1][j] else: dp[i+1][j]=mi[i]+a[i+1][j]+a[i][j] mi[i+1]=min(dp[i+1]) print(mi[-1])