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 << 50 B = [(INF, -1)] for i in range(N - 1): acc = INF idx = None for j in range(M): if A[i][j] + A[i + 1][j] < acc: acc = A[i][j] + A[i + 1][j] idx = j B.append((acc, idx)) dp = [[INF for _ in range(M)] for _ in range(N + 1)] dp[0] = [0] * M for i in range(N): mc, mi = B[i] for j in range(M): dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + A[i][j]) dp[i + 1][mi] = min(dp[i + 1][mi], dp[i][j] + mc) print(min(dp[N]))