import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); if (n == 1) { System.out.println(0); return; } int m = sc.nextInt(); int[][] values = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { values[i][j] = sc.nextInt(); } } long[][] dp = new long[n][m]; for (int i = 0; i < m; i++) { dp[0][i] = values[0][i]; } for (int i = 1; i < n; i++) { long min = Long.MAX_VALUE; for (int j = 0; j < m; j++) { dp[i][j] = dp[i - 1][j] + values[i][j]; min = Math.min(min, dp[i][j]); } for (int j = 0; j < m; j++) { dp[i][j] = Math.min(dp[i][j], min + values[i][j]); } } long ans = Arrays.stream(dp[n - 1]).min().orElse(0L); System.out.println(ans); } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }