#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)n;i++) #define Rep(i,h,t) for(int i=(int)h;i<(int)t;i++) #define Rrep(i,n) for(int i=(int)n;i>=0;i--) #define RRep(i,h,t) for(int i=(int)h;i>=(int)t;i--) #define UNIQ(v) v.erase(unique(v.begin(),v.end()),v.end()); #define pb push_back #define Fi first #define Se second template<typename T> bool chmax(T& vmax,const T& v) { if(vmax<v) { vmax=v; return true; } return false; } template<typename T> bool chmin(T& vmin,const T& v) { if(vmin>v) { vmin=v; return true; } return false; } using namespace std; using ll=long long; using lb=long double; using veci=vector<int>; using vec2i=vector<vector<int>>; using vecll=vector<ll>; using vec2ll=vector<vector<ll>>; using p2i=pair<int,int>; using p2ll=pair<ll,ll>; using m2i=map<int,int>; using m2ll=map<ll,ll>; using msi=map<string,int>; ll gcd(ll a,ll b) { if(b==0) { return a; } return gcd(b,a%b); } ll lcm(ll a,ll b) { return ll(a/gcd(a,b))*b; } void yn_out(bool b,string yes_out="Yes",string no_out="No") { cout<<(b ? yes_out : no_out)<<endl; } ll ceilll(ll a,ll b) { return (a+b-1)/b; } const int MOD=1e9+7; //const int MOD=998244353; const int dirx[4]={1,0,-1,0}; const int diry[4]={0,1,0,-1}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); //cout<<fixed<<setprecision(9); int N,M; cin>>N>>M; if(N==1){ cout<<0<<endl; return 0; } ll INF=1e16; vec2ll A(N+1,vecll(M,0)); rep(i,N){ rep(j,M){ cin>>A[i+1][j]; } } vec2ll dp(N+1,vecll(M,INF)); rep(j,M){ dp[0][j]=A[0][j]; } rep(i,N){ ll mn = INF; rep(j,M){ dp[i+1][j]=dp[i][j]+A[i+1][j]; chmin(mn,dp[i+1][j]); } rep(j,M){ chmin(dp[i+1][j],mn+A[i+1][j]); } } cout<<*min_element(dp[N].begin(),dp[N].end())<<endl; return 0; }