import sys # import pypyjit import itertools import heapq import math import bisect from collections import deque, defaultdict from functools import lru_cache, cmp_to_key # for AtCoder Easy test if __file__ == 'prog.py': pass else: sys.setrecursionlimit(10 ** 6) # pypyjit.set_param('max_unroll_recursion=-1') input = sys.stdin.readline def readints(): return map(int, input().split()) def readlist(): return list(readints()) def readstr(): return input()[:-1] N, M = readints() A = [readlist() for _ in range(N)] if N == 1: print(0) exit() INF = 1 << 30 dp = [[None for _ in range(M)] for _ in range(N)] dp[0] = A[0] for i in range(1, N): m = INF for j in range(M): dp[i][j] = dp[i - 1][j] + A[i][j] m = min(m, dp[i][j]) for j in range(M): dp[i][j] = min(dp[i][j], m + A[i][j]) print(min(dp[N - 1]))