import std; void main(){ auto a = readln.split.to!(long[]); auto N = a[0]; auto M = a[1]; long[][] A; for(auto n = 0; n < N; n++){ A ~= readln.split.to!(long[]); } if(N == 1){ writeln(0); return; } auto cost = new long[][](N + 1, M); for(auto n = 0; n < N; n++){ long min_cost = cost[n].minElement; for(auto m = 0; m < M; m++){ if(cost[n][m] == min_cost){ cost[n + 1][m] = min_cost + A[n][m]; }else{ cost[n + 1][m] = min(min_cost + A[n][m] + A[n - 1][m], cost[n][m] + A[n][m]); } } } //stderr.writeln(cost); writeln(cost[$ - 1].minElement); }